The UNIX and Linux command interpreter or shell is the program users interact with in a terminal emulation window. The terminal emulation window can be one in the workstation's Graphical User Interface - gnome-terminal on Linux or dtterm on Solaris. Alternatively, it can be an application such as telnet, secure shell client or PuTty on a Windows PC that's logged into Linux or UNIX over the network.
The shell used in the School of Computer Science & Informatics is bash - Bourne Again Shell. There are other shells available such as the Bourne Shell, the C-Shell and the TC-Shell, and you can choose to use a different shell if you prefer. They all have similar characteristics but each has its own particular features. This Note assumes you are using bash.
Bash has the following features:
bash-2.05$- a dollar symbol preceded by ``bash'' and the bash program's version number.
bash-2.05$ cdis used to change the current directory to a different location in the UNIX or Linux file system.
bash-2.05$ lsruns the program ls which reads the current directory and lists the names of its files.
The search path includes the current directory, your home directory and its subdirectory ``linuxbin'' or ``solarisbin'' (depending on whether you are logged in to Linux or Solaris). You can write your own programs and invoke them simply by typing their names. If you store such a program in the directory ``linuxbin'' or ``solarisbin'' as appropriate, it will be found and run no matter what your current directory is.
bash-2.05b$ cd ~/linuxbinchange the current directory to ``linuxbin'' in your home directory. (The tilde character ~ means your home directory to the shell.
Some commands need more than one argument. E.g.
bash-2.05b$ cp fileA fileBis the copy command cp with two filename arguments; ``fileA'' is copied to a new file, ``fileB''.
Some commands have flag or option argument strings usually beginning with ``-'' or ``-''. The flags modify the behaviour of the program being invoked:
bash-2.05b$ ls -ltmakes ls give a long listing of the files sorted by time of creation.
bash-2.05b$ ls -l *.cwill give a directory listing of the files with names ``anything.c'' (conventionally C program source files).
The symbol ``
'' is used to redirect standard input from a file and the symbol ``
'' is used to redirect standard output to a file. For example:
bash-2.05b$ catmakes cat reads from file ``fileA''. It outputs the file's contents to the terminal or screen.fileA
bash-2.05b$ catreads from ``fileA'' and writes to ``fileB''.fileA
fileB
bash-2.05b$ lspipes the output of ls (viz., your filenames) into the word count program wc. The ``-w'' flag tells wc to count the number of words in its input. So the above command counts the number of files in your directory.wc -w
bash-2.05b$ alias xx=exitsets up an alias ``xx'' to stand for the command exit.
bash-2.05b$ x="hello world"prints ``hello world'' on the screen. Some variables are pre-set, e.g. $HOME is your home directory. Type set to see a list of assigned variables.
bash-2.05b$ echo $x
bash-2.05b$ man bashto view its manual page.
bash-2.05b$ catcreates a bash script file in your ``linuxbin'' directory. The chmod command changes its protection mode to make it executable.~/linuxbin/countfiles
#!/bin/bash
lswc -w
^D
bash-2.05b$ chmod +x ~/linuxbin/countfiles
The first line of the script tells Linux that the script is written in the Bourne again Shell language (UNIX and Linux scripts can be written in any application language), while the second line tells the Shell to run the directory listing command, ls, and pipe its output to the word count program, wc. Now
bash-2.05b$ countfileswill execute the script and count and output the number of files in your directory,
bash-2.05b$ sort bigfileThe "&" puts the sort program into the background and the shell is available immediately for other commands. The shell prints the job control number (``1'' in this case) and the process identity number (``3470'').sortedfile &
1
3470
bash-2.05b$
The special character ``^Z'' (Control-Z) can be used to suspend a program which is running in the foreground:
bash-2.05b$ sort bigfileYou may now use command bg to put the program into the background or fg to continue it in the foreground. If you have more than one job running in the background or suspended, you can refer to them by their job number. Sosortedfile
^Z1
+ Stopped sort bigfile >sortedfile
bash-2.05b$
fg %2bring job number 2 into the foreground while
kill %1terminates job number 1.
The command
bash-2.05b$ jobslists the status of all stopped or background jobs along with the job number (1,2,3...).
If a backgound job needs terminal input, it will stop until you bring it into the foreground.
bash-2.05b$ historylists the remembered commands along with a reference number, e.g:
515 cd .Intro
516 ls -ltd 302*
517 latex 302
518 latex 302
519 dvips -Ppdf -G0 -ta4 302
520 ps2pdf 302.ps
521 acoread 302.pdf
In a workstation's terminal emulation windows, you can cut and paste from the history to rerun a command. You can also use the symbol ``!'' to rerun any command from the history:
bash-2.05b$ !518reruns command number ``518'' from the history.
bash-2.05b$ !psreruns the last command starting ``ps...''.
bash-2.05b$ !!reruns the last command.
See the manual page on bash for more details (type man bash).
Bash has an
additional mechanism which allows you to recall and edit previous commands
using the keyboard up-arrow key
.
If you press up-arrow, the last command re-appears on the terminal. Press up-arrow
again to get earlier commands. To rerun the command, press
.
To amend the command before rerunning it, use the delete key to
remove characters from the end or use the back-arrow
to reposition the cursor to delete or insert characters within the command.