What is a Shell?

Shell: Takes input from the user in the form of commands, processes it, and then gives an output.

Basic Form of Shell:

while (read command) {
  parse command
  execute command
}

Shell Families

Two main shell families in UNIX:

  1. The Bourne Shell: Prompt is $.
  2. C Shell: Prompt is %.

Note: Z Shell (zsh) is inspired by both Korn shell and Trusted C Shell, and thus doesn’t fit within a single shell family.

History

The original Bourne shell distributed with V7 Unix in 1979.

Berkeley C shell (csh) offered features that were more pleasant for interactive use.

David Korn at Bell Labs enhanced the Bourne shell by adding csh-like features to it.

Today, the POSIX standard defines the “standard shell” language and behavior based on the System V Bourne shell, with a selected subset of features from the Korn shell.

The Free Software Foundation, in keeping with its goal to produce a complete Unix work-alike system, developed a clone of the Bourne shell written from scratch, the Bourne-Again Shell.

Shell Source Code

ShellLocation
Bashftp://ftp.gnu.org/gnu/bash
Bash source code patchesftp://ftp.gnu.org/gnu/bash/bash-3.0-patches
Ksh93http://www.research.att.com/sw/download/
The Z Shellhttp://www.zsh.org
The Public Domain Korn Shellhttp://web.cs.mun.ca/~michael/pdksh/
Tcshhttp://www.tcsh.org

Common Features in Bash, Korn, and C Shells

SymbolDescription
>Redirect output.
>>` | Append to file. | |<| Redirect input. | | ``<<“Here” document (redirect input).
|Pipe output.
&` | Run process in background. | |;| Separate commands on same line. | | ``~Home directory symbol.
*` | Match any character(s) in filename. | |?| Match single character in filename. | | ``[ ]Match any characters enclosed.
()` | Execute in subshell. | || Expand elements in list.a | | ``' 'Substitute output of enclosed command.
" "` | Partial quote (allows variable and command expansion). | |’ ’| Full quote (no expansion). | | ``\Quote following character.
$ var` | Use value for variable. | |$$| Process ID. | | ``$0Command name.
$ n` | n th argument (0 ≤ n ≤ 9). | |$*| All arguments as simple words. | | ``#Begin comment.
CommandDescription
bgBackground execution.
breakBreak from loop statements.
cdChange directory.
continueResume a program loop.
echoDisplay output.
evalEvaluate arguments.
execExecute a new shell.
fgForeground execution.
historyList previous commands.
jobsShow active jobs.
killTerminate running jobs.
shiftShift positional parameters.
suspendSuspend a foreground job (such as a shell created by su).
timeTime a command.
umaskSet default file permissions for new files.
unsetErase variable or function definitions.
waitWait for a background job to finish.

Different Features in Bash, Korn, and C Shells

bashkshtcshMeaning/action
$$%Prompt.
>|>|>!Force redirection.
>>!Force append.
> file 2>&1> file 2>&1>& fileCombine stdout and stderr.
>& file>& fileCombine stdout and stderr.
' '' '' 'Substitute output of enclosed command.
$()$()Substitute output of enclosed command. (Preferred form.)
$HOME$HOME$homeHome directory.
var = valuevar = valuesetvar=valueVariable assignment.
export var = valexport var = valsetenv var valSet environment variable.
${ nn }${ nn }More than nine args can be referenced.
"$@""$@"All args as separate words.
$#$#$#argvNumber of arguments.
$?$?$statusExit status.
$!$!Last background Process ID.
$-$-Current options.
. file. filesource fileRead commands in file.
aliasx=yaliasx=yalias x yName x stands for y.
bashkshtcshMeaning/action
cd ~-cd ~-popd/pushdSwitch directories.
popd/pushdpopd/pushdSwitch directories.
donedoneendEnd a loop statement.
esacesacendswEnd case or switch.
exit [ n ]exit [ n ]exit [( expr )]Exit with a status.
for/dofor/doforeachLoop through values.
echo -Eprint -rglobIgnore echo escapes.
hashalias -thashstatDisplay hashed commands (tracked aliases).
hash cmdsalias -t cmdsrehashRemember command locations.
hash -rPATH=$PATHunhashForget command locations.
historyhistoryhistoryList previous commands.
fc -sr!!Redo previous command.
fc -s strr str! strRedo command that starts with str.
fc -s x = y [ cmd ]r x = y [ cmd ]! cmd :s/ x / y /Edit command, then execute.
if ((i==5))if ((i==5))if ($i==5)Sample if statement.
fifiendifEnd if statement.
ulimitulimitlimitSet resource limits.
pwdpwddirsPrint working directory.
readread$<Read from standard input.
trap INTRtrap INTRonintrIgnore interrupts.
unaliasunaliasunaliasRemove aliases.
until/dountil/doBegin until loop.
while/dowhile/dowhileBegin while loop.

Learning a Shell

Shell Script

Shell Script: A regular text file that contains shell or UNIX commands.

Shebang (#!)

Shebang: Specifies shell to execute a program.

Examples

Recommended Extensions

Remember: File extensions don’t matter in UNIX, but can be helpful for organization.

Comments

The hash symbol (#) can be used to create comments.

Example:

#!/usr/bin/env bash
echo "Hello, world!" # This is a comment