Choosing and Using a Shell


The shell is perhaps the most underappreciated feature of a modern Linux system. New users try to avoid it, preferring the GUI tools that they are used to. Old-timers are wedded to the shell they learned at school. There's no real reason to give the shell a second thought, is there? Of course there is!

By default, SUSE Linux (and pretty much all the other Linux distributions) installs the bash (Bourne Again SHell) shell as the standard command-line interface. There's certainly no reason not to take this fine choice and run with it. But this is Linux, and there's absolutely no reason not to explore some or all of the alternatives to bash included in SUSE Linux. This section will introduce you to these options, tell you how to change shells temporarily and permanently, and then offer pointers on how to use a shell productively.

Note

Learn even more about using shells, including writing shell scripts to automate tasks, in Chapter 8, "Shaking Hands with Your Shell."


Cool Stuff That All Shells Have in Common

The shell is not just a keyboard-based program launcher. It is a powerful tool you can use to get lots of things done quickly if you know how to use it. Some shells are designed for running scripts to automate tasks, and others work best interacting with its favorite user you! Following are some of the things that all shells have in common:

  • Command history The shell stores every command you enter (up to a limit, which is configurable). This feature lets you easily access and repeat commands from the history. Press the up-arrow key to see the last command and keep pressing to view each previous command. Pressing the down-arrow key moves forward in time, as it were, returning you back to an empty prompt. To reuse a command, locate it and press Enter to run the command.

    This works over sessions, too. Even if you run the shell only once a month (or once a decade), the command history stays current.

  • Filename completion Another terrific tool for lazy typists. Start typing a filename and press the Tab key.

    The shell checks the path for files that start with the keys you typed, and a suggestion appears.

    This is especially helpful when performing actions on files with long names, or deep in a directory. Some shells will also complete commands. See the sections on running tcsh and zsh later in the chapter.

  • Command-line editing Did you mistype a letter in a command and get an error? Don't retype the whole thing; use the command history and left- and right-arrow keys to get to the error, or change the options.

  • Many utilities to work with Tools such as sed and awk can help with commands and scripts.

  • Aliasing Another typing shortcut, similar to a symlink. One command (or set of letters) stands in for another. This is set up in the shell configuration file. Among other things, aliasing allows bash to run when you type sh and the tcsh shell to run when you call csh.

  • Background processing If one command is taking a long time to complete, you can move it to the background and run another command.

  • Multiple sessions You can have more than one shell (and multiple instances of the same shell) open at the same time, even without a graphical interface, and switch back and forth.

  • Programmable everything The shell is a programming environment with languages on par with any other programming language.

These are just some of the advantages of most all shells. But how does one shell differ from another? We will look at the three most popular shells bash, tcsh, and zsh in some detail, and then look at Midnight Commander (mc), a specialized shell for managing files and directories. Finally, there is a very brief introduction to the Korn (ksh) and the Almquist (ash) shells.

Running bash

Just about every Linux user has used bash at least once. As one of the original GNU projects, it has been in development since 1987. Version 3.0 was released in July 2004 and is included in SUSE Linux Professional 9.2. As you might guess from the name, bash (Bourne Again Shell) is the logical successor to the venerable Unix shell (sh) written by Steve Bourne. This is a shell best adapted for running scripts to automate tasks.

You can tweak bash's performance by editing the .bashrc configuration file. Root can edit the systemwide .bashrc, and each user on the system may have his or her own configuration file, although a user cannot override the system configuration.

Looking at a configuration file provides both a sample of the kinds of things you can do with a shell and offers a glimpse at the syntax each shell uses to perform its tasks. The default system configuration in SUSE Linux, bash.bashrc, is located, like other configuration files, in the /etc directory. Let's look at some of the settings defined here.

This section lets you display directory listings in different colors.

The colors are set in another file, DIR_COLORS. In this setting, bash looks for DIR_COLORS first in the home directory, then in /etc.

# # Colored file listings # if test -x /usr/bin/dircolors ; then     #     # set up the color-ls environment variables:     #     if test -f $HOME/.dir_colors ; then     eval `dircolors -b $HOME/.dir_colors`     elif test -f /etc/DIR_COLORS ; then     eval 'dircolors -b /etc/DIR_COLORS'     fi fi 

Note the hash mark (#) in front of some of the lines. This indicates a comment line. The line is not processed by the shell, but usually describes what the next batch of code is supposed to do.

This next section defines some aliases. As mentioned before, aliases are command-line shortcuts that allow you to substitute one command for another.

# # Set some generic aliases # alias rd=rmdir alias md='mkdir -p' alias which='type -p' alias rehash='hash -r' alias you='yast2 online_update' 

This alias is a nice one. If you often mistype a command, you can specify a little reminder. You may think that the opposite of the mount command to display a drive or partition is unmount, but really it's umount (no N). This alias tells bash to remind you of the right command every time you add the extra N. Sure, you could just have unmount replace umount on your system, but what if you are working on someone else's system that does not have this alias? You may just forget the real command.

With this method, you might train your fingers to type the right thing after a few gentle nudges.

alias unmount='echo "Error: Try the command: umount" 1>&2; false' test -s $HOME/.alias && . $HOME/.alias fi 

This section modifies how the command history is handled, by making sure the History file doesn't get too big with many duplicate listings of the same file.

    # Do not save dupes and lines starting by space in the bash history file     HISTCONTROL=ignoreboth     if test "$is" = "ksh" ; then     # Use a ksh specific history file and enable         # emacs line editor         HISTFILE=$HOME/.kshrc_history         VISUAL=emacs     fi     ;; esac 

SUSE recommends not editing this default system file, because it can be overwritten in a system update. With this bit of code, bash checks for a local configuration file edited by the system administrator, saved as /etc/bash.bashrc.local.

if test "$is" != "ash" ; then     #     # And now let's see if there is a local bash.bashrc     # (for options defined by your sysadmin, not SuSE Linux)     #     test -s /etc/bash.bashrc.local && . /etc/bash.bashrc.local fi 

As noted at the beginning of this section, bash is a shell environment for scripting. Other shells are designed to be more interactive. Let's look at the leading example: the C shell.

Running tcsh

Bill Joy (later to become a founder of Sun Microsystems) wrote the original C shell in college. He aimed to provide a more interactive work environment, with a syntax more like the C programming language.

Interactive shells are focused on directly processing commands you type in, rather than running scripts or files. Script-oriented shells such as bash can be used interactively, but it is rarely a good idea to make a C shell run scripts.

The version of the C shell included in SUSE Linux is called tcsh, a tribute to the TENEX mainframe operating system. It has programmable command completion and a referable command history.

The best thing about tcsh, though, is its command spell checker.

If you type frotune at a tcsh prompt, it will ask if you mean fortune. A yes answer gives you a fortune cookie.

Running zsh

The Z shell strives to be the best of both worlds, enabling you to safely write scripts or use it interactively. It is based on the Korn shell (ksh) written by David Korn, with tons of extra features. Many folks love zsh to bits for its power and flexibility, but with great power comes great complexity. This shell's Info file has 21,530 lines of documentation in 11 subnodes. We won't cover all of that here.

zsh has virtually all the neat features previously listed and works quite well with the default configuration file (etc/zshrc).

Some features, such as command spell check, are not set by default, but it is a fairly easy task to edit them.

This shell also checks your default text editor, and if it is emacs or vim, it will adopt that editor's key bindings (aka keyboard shortcuts) in addition to its own.

Managing Files with Midnight Commander

Whatever shell you choose for your everyday use, you may find the Midnight Commander a handy in-between tool for moving files around. In between? Suppose you don't need to load a full GUI desktop to move a batch of files, but even all the wildcards and completion shortcuts offered by your shell won't let you easily copy a set of files from different directories into one other directory. You need something else something that will display two directories side by side and let you drag or move things from one place to another. You need Midnight Commander!

To open Midnight Commander, type mc at the shell prompt. Figure 5.1 shows the initial default display of your home directory on both sides of the screen. Use the arrow keys to navigate in the left pane. Use the Tab key to move from one side to the other, and then move around. Select a file or folder and press Enter to open that item. Mouse support is excellent; click any menu at the top to see your choices. Along the bottom are the F-key bindings: pressing F1 brings up the Help file, F2 displays a context-sensitive menu of choices for the selected file or directory, and so on.

Figure 5.1. The Midnight Commander shell.


To copy a set of documents with the .sxw extension from ~/documents to a directory on a remote server called /backup/mikemc, follow these steps:

1.

In the left pane, use the arrow keys to navigate to the /Documents directory. Press Enter to open this directory.

2.

Tab to the right pane. At the bottom of the Midnight Commander, you will see a flashing cursor. This is a shell prompt. Type cd /backup/mikemc to navigate to this directory. You could use the parent folders, marked .., to find the directory, but this is faster.

3.

Tab back to the left pane. Press Alt-+ to open the Select Group dialog. Type *.sxw and press Enter to select all files with the .sxw extension.

4.

Press F5 to open the Copy dialog box. Confirm that the correct files are going to the correct directory. Select any options you want by navigating with the Tab key and selecting with the spacebar. Press Enter to complete the operation.

Midnight Commander is a program ripe for exploration. The first few times you work with it, play around a little. Go to a menu, or press an F key to see what a command does.

The help files are indeed helpful! This is a full-featured file manager: View hidden files if you like, access FTP sites, and upload or download files (at the same time, if you want to), run executables, and change permissions (if you can) and owners with the ChMod and ChOwn commands. In short, just about everything you can do with a file in a shell, you can do with Midnight Commander. It even has a command history.

Tip

The KDE Konsole lets you open a new Midnight Commander session from the Session menu. Choose regular or Root (with your password).


ksh and ash

The other shell choices may be less popular, but are still valid. Here's a quick overview:

  • Korn shell (ksh) This shell, written by David Korn, was the first popular shell to try to combine the scripting features of the Bourne shell and the interactive features of the C shell. AT&T tried to turn ksh into a commercial product, with limited success. There are two versions of the "pure" Korn shell: ksh88 and ksh93. The version included with SUSE Linux is the public domain pd-ksh. It has reproduced most of the features of ksh88, with a few new ones of its own.

    The Z shell is also one of the Korn shell descendants, and if you know ksh well, you should be able to function well with zsh.

  • Almquist shell (ash) This lightweight Bourne-like shell has but 17 built-in commands, so as a shell it's perfect for embedded and other tight spaces and login shells. In fact, it is the SUSE login shell.

Choosing a Shell

You now know a little bit about each shell. How do you decide which to use? The easiest thing is, of course, to stick with what you know. If you have used a shell before, there should be some compelling reason to switch. Similarly, if you don't ever intend to do anything fancy with your shell, you might as well stick with bash. You can certainly do very fancy things with bash, but why change from the default?

Bash is also a great tool to learn scripting from. Resources abound on the Net to help you do that, and you'll learn more about that in Chapter 8 as well. If you don't want to write scripts, use an interactive shell such as tcsh, pd-ksh, or zsh.

If you are a lousy typist, you should certainly consider one of the shells with a spell checker. tcsh spell checks by default; zsh spell checks with a very quick configuration file edit. The fancier you want to get with file and command completion, the more you'll probably want zsh.

As recommended at the beginning of this section, try out all the shells, at least once. All you have to do is type the name of the shell at any other shell prompt. When you're done and want to return to bash, type exit. Look over the documentation. All the shells have extensive man or info pages. After you've settled on a shell, run the Change Shell (chsh) program to identify your choice. Then take some time to read those docs thoroughly.

Now that you've settled one of the interminable Linux wars to your satisfaction, you can choose a text editor.

Running YaST from the Shell

YaST is a tool older than most GUI desktop environments, and it runs beautifully from any shell. Log in as the SuperUser with the su command, and then type yast at the prompt. Figure 5.2 shows the opening screen in a KDE Konsole.

Figure 5.2. YaST works just as well from the shell as from a GUI environment.


This is especially useful in an emergency, when the system won't boot to the GUI login screen. To restore your system from a previous backup (see Chapter 20, "Managing Data: Backup, Restoring, and Recovery," for more information on backing up and restoring your system), follow these steps:

1.

Run YaST by typing yast at the rescue prompt.

2.

Use the arrow key to select System from the listing on the left.

3.

Tab to the module section on the right, and then arrow down to Restore System.

4.

Follow the instructions on the page or view details in the "Using System Restore" section of Chapter 20.

To maneuver around the menus and choices, use the accelerator keys highlighted on each screen. Press the Alt key with the highlighted letter to move to the item you want. For example, in any screen, press Alt+H to get Help on that screen. When finished, press Alt+Q to Quit and return to the shell prompt.

All YaST modules that are in the GUI version work in the command-line version as well, although Online Update (YOU) is dependent on the state of your Internet connection.




SUSE Linux 10 Unleashed
SUSE Linux 10.0 Unleashed
ISBN: 0672327260
EAN: 2147483647
Year: 2003
Pages: 332

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net