Shell Basics
The shell is the program that interprets your instructions (from keyboard or file). There are several alternatives available on UNIX but the default on our machines is bash.
Special characters
~
shorthand for home directory eg:'ls ~gjc'
will list gjc's home directory$
expand a shell variable eg:'$HOME'
will expand to your home directory*
and?
globbing characters<
,>
and|
redirection characters
Shell variables
- Hold information that can be accessed by the shell. For example PATH tells the shell where to look for executable files.
- Set simply by
VARIABLE=value
Globbing (wildcards)
- Globbing is the expansion of metacharacters into matching filenames
- Globbing is handled by the shell, not the application
*
matches any character any number of times.?
matches any character once.
Example
% ls file1 file1.old file2 file2.old file3 % ls file? file1 file2 file3 % ls file* file1 file1.old file2 file2.old file3 % ls file*.* file1.old file2.old
ls
never sees*
or?
, it sees the expanded list of files.
is just another character matched literally
Environment variables
-
Shell variables that can be accessed by the
shell or any program started from the shell.
- Set using the
export
command.
- Can be listed by the
printenv
command.
Example
export
command.printenv
command.To tell the loader to check the current directory when looking for shared libraries.
export LD_LIBRARY_PATH=$PWD or export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH
Important environment variables
PATH | list of directories to search for executable files |
LD_LIBRARY_PATH | list of directories to search for shared libraries |
PWD | maintained by the shell: current working directory |
Initialisation Files
/etc/profile
- System wide initialisation file.
- Processed once every time you log in. Before your .bash_profile
.bash_profile
- Processed once every time you log in.
- Processed by xterm, gnome-terminal & konsole only if "login shell" is selected.
- Good place to set environment variables especially PATH.
.bashrc
- Processed every time you fork a new shell
- Not automatically processed when you log in.
- Processed by xterm, gnome-terminal & konsole only if login shell is not selected.
History
- The shell maintains a history of commands executed
- The previous command can be recalled by hitting the up arrow
- The history list can be searched using
<Ctrl-R>
Tab Completion
- Hitting the
TAB
key while typing will cause the shell to attempt to complete the word you are typing - While typing a command it searches your PATH for all matching executables
- While typing an argument it looks for matching files in your current directory
- If it finds more than one match it bleeps, hitting
TAB
again will display all the matches it has found
Previous section: Getting Started
Next section: Shell Process Control