The UNIX/Linux Shell

What we'll cover:

Commands (nothing new here)

What happens when you login? Changing your default shell

How the shell interprets your commands

The shell interprets your commands in a specific order
1. checks to see if it matches an internal shell command
Every shell has a few built in commands.
2. checks to see if the command is an alias for another command
The alias command will list all the aliases defined in your environment.

$ alias
alias mv='mv -i'
alias new='ls -lt|head'

3. searches the hard disk for the program based on your PATH

What is your path?

$ env
...
PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:\
/home/pattyo/bin:/sbin:/usr/sbin
...

The env command displays your environment.
This is also the environment you get when you su - to some other user on the system.

What happens when a command is not in your path?
$ fortune
bash: fortune: command not found
Use the whereis command to locate the command.
$ whereis fortune
fortune: /usr/games/fortune /usr/share/man/man6/fortune.6.gz
Now use the absolute (full) path name to execute the program.
$ /usr/games/fortune
Art is a lie which makes us realize the truth.
                -- Picasso

Shell Types

Major UNIX shells:

Metacharacters and Windcards

Wildcards and pattern matching

Asterisk * Question Mark ? Lists [ ] Exclamation Point ! Square Brackets for Ranges

The position defined by the location of the square brackets is expanded by the shell but to only one character.

$ ls /etc/rc.d/rc3.d/K[1-5]*
  K20bootparamd  K20rstatd  K20rusersd  K20rwhod  K28amd  K35dhcpd  K55routed
Remember, the symbolic links found in this directory are executed based on the run-level your system is moving to (S for start, K for kill).

Quoting Metacharacters

Shell Redirection and Piping File Descriptors When a process starts it has three file descriptors: Input Redirection

The mail program is instructed to take someletter as the standard input:

$ mail someuser < somefile

0 stdin: somefile (changed)
1 stdout (unchanged)-- the screen
2 stderr (unchanged)-- the screen

$ mail someuser -s "my subject" < somefile

Output Redirection
$ who > filename
The shell creates the new file, filename,  and connects the output of  the who utility to the new file.

NOTE: If filename already existed, it will be deleted as soon as the who command executes!

$ cal 10 2001 > thismonth
$ cat thismonth

    October 2001
Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
The cal command's descriptor table follows: >> Redirecting output and appending to an existing file Output redirection, following by input redirection
$ ls -l > ls.out
$ sort -n +4 < ls.out
The sort command is followed by the -n option for numerical data and the "+4" option which tells sort to sort the file based on the 4th column (*starting from 0). What will this command do? Try it and find out!
$ cd /tmp
$ echo 2 * 3 > 6  more stuff here
Now examine the contents of the file, 6, you just created:
$ cat 6
You instructed the shell to echo the number 2, list all the files in the current directory,  echo the number 3, then redirect everything to the file, 6.

What would be the effect of quoting this command the way you see below?

The shell simply echos your string to the terminal because it is ignoring the metacharacters in the command line between the quotes.

Look at the following three commands. What do they have in common?

$ cat < file1 > file2
$ cp file1 file2
$ cat file1 > file2
Using cat and Redirecting Output

The cat command is normally used to list the contents of a file

$ cat /etc/passwd
You can use cat with redirection to create a new file:
$ cat > newfile
some text here
some more text here
<Ctrl>d
Here are the descriptores for the cat > newfile example:
0 stdin (unchanged)-- keyboard
1 stdout: newfile (changed)
2 stderr (unchanged)-- screen
This example creates a file called file3 which contains the combined contents of file1 and file2.
$ cat file1 file2 > file3<
Is it possible to concatinate two files into one file and retain the same name as one of the original files?

example 1:

example 2: 2> Error Redirection

By default, the standard error ouput of a command goes to the screen or the system console.

If you want to save the error messages you can append the redirector to standard output of your command.

$ command 2> file1
Lets try catting a file that doesn't exist:
$ cat dog
cat: dog: No such file or directory
Now save the error output to a file: 2> for Redirecting Error Output to the "Bit Bucket"

Sending standard error to the bit bucket. /dev/null is the bit bucket. What goes in, never comes out. You won't see the error message on the screen.

$ cat dog 2> /dev/null
Redirecting and appending error output

Simply add the '2' file descriptor.

command 2>> filename
Combined Redirection    Typical Combined Redirection    Association Order does matter sometimes!
Connecting Commands with Pipes Remember our earlier examples of input and output redirection:
$ ls -l > ls.out
$ sort +4 < ls.out
Here is a way to put those two commands together using a pipe:
$ ls -l | sort +4
Using Filters Using the tee Command to Split Output

To see the output on the screen and also save it to a file:

$ date | tee mynow_file
The tee command can be used for capturing a snapshot of information at a specific point in a pipe.

You can send both stderr and stdout through a pipe.

$ command 2>&1 | tee logfile

The Backslash for Escaping Special Characters

Since the single quote is a special character, how do you echo a string that contains a quote such as this:

$ echo my name's patty
You would have to escape the quote.
$ echo my name\'s patty
my name's patty
Line Continuations

This shell feature is used when the options and arguments appended to a single command cause you to type past the lenght of the line.

It is typically used in shell scripting to make the script more readable.

$ cmd continued_cmd \
> continued_cmd
Have you ever wondered why your prompt looked like this after typing a command?
>
The return key is just another character that can be escaped. You might have hit the backslash by accident. In this case, the shell is waiting for more input.
You escaped the return character.

What if you really want the backslash in your output.

$ echo i like the \ character
$ echo i like the \\ character