UH Navigation bar CS VNET NSM UH SEARCH

Computer Science Department Unix Primer

The Shell

In unix, your prompt is handled by a program called the shell. There are several variants of shells available to unix users, the most popular are sh and csh, which have more powerful modern derivatives called bash and tcsh. We'll be working with bash, or the Bourne-Again SHell, since it is the default shell in Linux, and one of the most popular for that reason.

If you are not running bash in your Computer Science Account, you can change your shell with the following commands:

Solaris: passwd -r nis -e
Linux: ypcshsh

If you are on bayou, you can change your shell with the standard unix command, chsh

$ chsh

Useful Features of Shells

Command Completion

Bash (and tcsh) have a very useful command completion feature that can save a great deal of typing. Command completion, simply put, finishes typing the command you've started to type. Pressing the TAB key while entering a command will trigger a routine in the shell that searches for possible completions. If no unique completions are found, the shell will simply beep at you. If you hit TAB a second time, the shell will display a list of all possible completions.

Filename arguments are handled the same way. Start typing a path, either relative (from current directory) or absolute (beginning with a /), then hit TAB to get possible completions.

Wildcards

Another time-saving feature of shells is in the wildcard feature. For those with experience in regular expressions, the concept is similar. A wildcard is simply a short hand substitution for a large set of possible filenames.

For example, the command,

$ ls *.txt

will apply the ls command to all files in the current directory that end in the characters, ".txt".

Common wildcard sequences are demonstrated below. For each of the examples, assume there is a directory with the contents,

$ ls
data01.dat  data05.dat  data09.dat  text01.txt  text05.txt  text09.txt
data02.dat  data06.dat  data10.dat  text02.txt  text06.txt  text10.txt
data03.dat  data07.dat  data11.dat  text03.txt  text07.txt  text11.txt
data04.dat  data08.dat  data12.dat  text04.txt  text08.txt  text12.txt
* (asterisk)
As in the example above, * will substitute for any set of characters against a set of files.
$ ls *02.*
data02.dat  text02.txt
? (question mark)
The question mark will substitute for exactly one character in a filename.
$ ls data0?.dat
data01.dat  data03.dat  data05.dat  data07.dat  data09.dat
data02.dat  data04.dat  data06.dat  data08.dat
[] (braces)
Characters included in the braces are substituted individually in an either or fashion. A wildcard of [abc] will match either an a, a b, or a c at the appropriate place in the substitution.
$  ls data0[567].dat
data05.dat  data06.dat  data07.dat

Lastly, wildcards can be combined to create more powerful substitutions.

$ ls *0[567]*
data05.dat  data06.dat  data07.dat  text05.txt  text06.txt  text07.txt

Command Editing

The command line interface in bash and tcsh (as well as most modern derivatives provide powerful features to edit the command line as in a modern word processor. Use the right and left arrow keys to move the cursor forwards and backwards in the command line to insert or delete information.

In addition, the ^a sequence will send the cursor to the beginning of the command, and the ^e sequence, to the end. ^d works as the delete key, which deletes characters to the right of the cursor rather than to the left.

Cutting and pasting text is also possible, ^k cuts from the cursor to the end of the line, and ^y pastes (yanks) the text back.

These special editing commands are known as the emacs key bindings, since they match the control sequences in the emacs text editor.

Command History

One final keypress-saving feature in most modern shells is a command history. Pressing the up arrow will take you back through previous command lines one at a time, which can be especially useful to re-run a complicated command. Combined with the above command editing features, one can easily tweak a previously run command by changing certain arguments. Special escape sequences (starting with the ! or bang character) allow you to substitute information from previous command lines into your current one.

Shell Customization

Customization of the shell environment can be achieved by setting certain special Shell variables. For example, all commands accessible to the shell must either be explicitly run from their place in the filesystem or found in the $PATH environment variable.

$ echo $PATH
/usr/sfw/bin:/usr/ccs/bin:/usr/bin:/bin:
$ export PATH=""
$ ls
-bash: ls: No such file or directory
$ /bin/ls
a.txt  b.txt
$ export PATH="/usr/bin:/bin"
$ ls
a.txt  b.txt

Another useful customization is to change the shell's default prompt. In bash, the prompt can be made to display all sorts of useful information back to the user with every command. One very common prompt string substitution is to display the current path in the string. Using the \w sequence in the PS1 environment variable (bash only) will do this.

$ echo $PS1
$
$ export PS1="\w \$ "
~ $ pwd
/home/user
~ $ export PS1="\h [\w] \$ "
turing [~] $ 

Shell Scripting

Shell scripts are a useful way to rerun commonly used sequences of commands. The most common shell script is a login or profile script. This is a file that is run at the beginning of a user's session that makes their customizations permanent across sessions. For bash, this is typically the .bash_profile and/or .bashrc files in the home directory.

In these files, it is possible to set environment variables as shown above, define aliases, and even run certain useful commands. It should be pointed out, however, that in order to use certain file transfer programs like scp and sftp, the .bashrc script should not cause any extra information to be printed to the screen.

# example .bashrc
# comments start with the hash mark like this

export PATH="/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin"

alias ls="/bin/ls -Fa --color=auto"
alias ll="/bin/ls -Fla --color=auto"
export PS1="\h [\W]\$ "
# For a bit of color, try one of these:
# export PS1="\[\e[1;32m\]\h[\w]\$\[\e[0m\] "
# export PS1="\[\e[1;33m\]\h[\w]\$\[\e[0m\] "
# export PS1="\[\e[1;34m\]\h[\w]\$\[\e[0m\] "

University of Houston State of Texas Privacy and Policies Homeland Security Compact with Texans Reporting Copyright Infringement Contact U H Feedback Site Map Statewide Search U H System