Shell
On path: User |
---|
Depends on | User |
---|
The shell is a user interface program that allows the user to give commands to the computer. The normal implication is that it is a Command Line Interface (CLI) although a Graphical User Interface (GUI) shell is possible too. The most common Unix shell, and the one you are most likely using, is the GNU Bash shell.
The shell will take user input, in the form of commands, and perform
operations based on those commands. bash
provides the ability to
launch programs with arguments, set-up
pipes, and even includes a scripting language.
The shell loads and maintains a set of environment variables. In bash
(for example) these can be revealed with the command set
.
Shells sometimes execute some commands directly as “builtins”; however most of the time they find and dispatch commands to other scripts or binaries.
Examples of execution patterns
The most usual behaviour when executing a file is for the shell to fork (create a new process) and load and execute the required application. The shell then waits for the child process to signal its demise, after which it resumes. This may give the appearance of changing what the shell is doing to the application and back again.
> my_prog
A bash
option can suppress the wait in the shell. (The shell can then start other programs without waiting for the first to terminate.)
> my_prog&
More elaborate behaviour might include starting more than one process in a single command, and linking these by redirecting standard streams through pipes.
> prog_a | prog_b
NOHUP
For further interest, you might like to look up the Unix
nohup
command.SIGHUP
(Signal Hang Up) is a Unix signal which indicates that a terminal/shell has closed – originally the ’phone line had been ‘hung up’ – and is sent to applications to close them down.nohup
allows such applications to outlive the shell.
Scripting
As well as interpreting and dispatching user commands, a shell will be able to interpret ‘simple’ software written in shell script. (“Simple” can really be quite sophisticated!)
A Unix shell script is fairly well integrated to the operating system and can (of course) execute other programs. No doubt many of your familiar ‘binaries’ are actually shell scripts – possibly then executing compiled code (or not).
If you want to play more with shell scripts (a useful skill!) then this looks to be quite a good tutorial. There is also now a shell scripting article here, which contains example shell scripts and explanations of their behaviour.
Aliases
Many shells allow you to set “aliases” for commands; these are text substitutions. For example, you could enter alias dir='ls -lsa'
which defines dir
as a particular way of ‘listing’ a directory. Such aliases persist for the life of that shell.
Caution: it is not usually advisable to alias the basic command names. For example some people
alias rm='rm -i'
– i.e. change the ‘remove’ command to make it ‘interactive’ (ask whether or not to delete each file). This is okay in itself but if you come to expect this behaviour, one day you will use a system where it isn’t set and might get a nasty surprise.
Configuring a shell
A typical shell will run a configuration file when it starts (and sometimes at other times). For example, the bash shell runs through a .bashrc
in your home directory when it is started.
- Files starting in ‘.’ are normally ‘hidden’: revealed by
ls -a
but not plain oldls
. - the ‘rc’ really stands for “run commands’
You can use this to customise your shells on start-up. This is a good place to set up aliases, paths, environment variables and the like. There are some details here or, if you prefer to watch, a video (5 mins.).
Most shells support something similar.