Shell Cheat Sheet



Variables


Shell variables
Borne C Shell Korn
num=21 set num=21 num=21
Environment variables
Borne C Shell Korn
NUM=1
export NUM
setenv NUM 1 export NUM=1
Variable Manipulation
Borne C Shell Korn
NAME=Rob
echo $NAME
echo ${NAME}ert
set NAME = Rob
echo $NAME
echo ${NAME}ert
NAME=Rob
echo $NAME or
print ${NAME}
echo ${NAME}ert or
print ${NAME}ert
typeset

Return to the top


Command Substitution


Assigning to Variables
Borne C Shell Korn
d=`date` set d = `date` d=$(date) or d=`date`
Printing Values
Borne C Shell Korn
echo $d echo $d
echo $d[1] $d[2]
echo $#d
print $d

Return to the top


Parameter Substitution


Substitution Sh Csh Korn Bash Description
${parameter} YesNoYesYes substitutes the contents of the parameter, which can be a variable name or digit indicating a positional parameter.
${parameter:-word} YesNoYesYes substitutes the contents of the parameter but if it is empty or undefined, it substitutes the word, which might contain unquoted spaces.
${parameter:=word} YesNoYesYes substitutes the contents of the parameter but if it is empty or undefined, it sets parameter equal to the word and substitutes word.
${parameter:?message} YesNoYesYes substitutes the contents of the parameter, but if it is empty or undefined, aborts the script and gives the message as a final error. message might contain unquoted spaces.
${parameter:+word} YesNoYesYes if parameter is not empty, it substitutes the word; otherwise it substitutes nothing.
${#parameter} YesNoYesYes substitutes the number of characters in the contents of parameter.
${parameter#pattern} YesNoYesYes if the regular expression pattern given is found at start of the contents of parameter, it deletes the matching characters and substitutes the remainder. The smallest possible match is deleted.
${parameter##pattern} YesNoYesYes if the regular expression pattern given is found at start of the contents of parameter, it deletes the matching characters and substitutes the remainder. The largest possible match is deleted.
${parameter%pattern} YesNoYesYes if the regular expression pattern given is found at end of the contents of parameter, it deletes the matching characters and substitutes the remainder. The smallest possible match is deleted.
${parameter%%pattern} YesNoYesYes if the regular expression pattern given is found at end of the contents of parameter, it deletes the matching characters and substitutes the remainder. The largest possible match is deleted.
Bash Only
${parameter:position} parameter is expanded starting from .position
${parameter:position:length} parameter is expanded starting from .position and extending length characters.
${parameter/pattern/replacement} the first match of pattern is replaced with replacement as parameter is expanded. If replacement is omitted, then the first match of pattern is replaced by nothing, that is, deleted.
${parameter//pattern/replacement} every match of pattern is replaced with replacement as parameter is expanded. If replacement is omitted, then every match of pattern is replaced by nothing, that is, deleted.
${parameter/#pattern/replacement} if the prefix (or first part) of the expanded parameter matches pattern, pattern is replaced with replacement as parameter is expanded. If the prefix is not matched, nothing is replaced, even if pattern is matched anywhere else in the expanded parameter.
${parameter/%pattern/replacement} if the suffix (or last part) of the expanded parameter matches pattern, pattern is replaced with replacement as parameter is expanded. If the suffix is not matched, nothing is replaced, even if pattern is matched anywhere else in the expanded parameter.
${!parameterprefix*} matches and expands to all previously declared variables beginning with parameterprefix.
${!parameterprefix@} same as above.
${#array[*]} substitutes the number of elements in array.

Return to the top


Filename Name Expansion


Single Character
Borne C Shell Korn
? ? ?
Zero or More Characters
Borne C Shell Korn
* * *
One Character of the Set
Borne C Shell Korn
[aeiou] [aeiou] [aeiou]
One Character of a Range
Borne C Shell Korn
[a-m] [a-m] [a-m]
One Character not in the Set
Borne C Shell Korn
[!aeiou] N/A [!aeiou]
Zero or One occurrences of the specified patterns
Borne C Shell Korn
[!aeiou] N/A [!aeiou]
One occurrence of the patterns
Borne C Shell Korn
N/A N/A @(patt1|patt2|patt3)
Zero, One, or more occurrence of the patterns
Borne C Shell Korn
N/A N/A *(patt1|patt2|patt3)
One, or more occurrence of the patterns
Borne C Shell Korn
N/A N/A +(patt1|patt2|patt3)
Any pattern except the specified patterns
Borne C Shell Korn
N/A N/A !(patt1|patt2|patt3)

Return to the top


I/O Redirection and Pipes


Input Redirect
Borne C Shell Korn
cmd < filey
cmd 0< filey
cmd < filey cmd < filey
cmd 0< filey
Output Redirect
Borne C Shell Korn
cmd > filey
cmd 1> filey
cmd > filey cmd > filey
cmd 1> filey
Error Redirect
Borne C Shell Korn
cmd 2> errors (cmd > out) >& errors cmd 2> errors
Append to a file
Borne C Shell Korn
cmd >> filex cmd >> filex cmd >> filex
Output and errors redirected to the same file
Borne C Shell Korn
cmd > both 2>&1 cmd >& both cmd > both 2>&1
Assign Output and Ignore NOCLOBBER
Borne C Shell Korn
N/A cmd >! filex cmd >| filex
Output and errors to Command
Borne C Shell Korn
cmd1 2>1 | cmd2 cmd1 |& cmd2 cmd1 2>1 | cmd2
Pipe the Output
Borne C Shell Korn
cmd1 | cmd2 cmd1 | cmd2 cmd1 | cmd2

Return to the top


Tilde Expansion


Home Directory of any User
Borne C Shell Korn
N/A ~username ~username
Home Directory of Current User
Borne C Shell Korn
N/A ~ ~
Current Working Directory
Borne C Shell Korn
N/A N/A ~+
Previous Directory
Borne C Shell Korn
N/A N/A ~-

Return to the top


Aliases


Creating an Alias
Borne C Shell Korn
N/A alias name value alias name=value
Listing Aliases
Borne C Shell Korn
N/A alias alias
Removing an Alias
Borne C Shell Korn
N/A unalias name unalias name

Return to the top


History


Set the History
Borne C Shell Korn
N/A set history = 25 automatic or HISTSIZE=25
Display a Numbered History List
Borne C Shell Korn
N/A history history, fc -l
Display a Numbered History List
Borne C Shell Korn
N/A history history, fc -l
Display a Portion of the List
Borne C Shell Korn
N/A history 5 history 5 10
history -5
Re-execute a command
Borne C Shell Korn
N/A !! (last command)
!5 (fifth command)
!v (last command starting with v)
r (last command)
r 5 (fifth command)
r v (last command starting with v)
Set Interactive Editor
Borne C Shell Korn
N/A N/A set -o vi
set -o emacs

Return to the top


Initialization Files


Executed at Login
Borne C Shell Korn
~/.profile ~/.login ~/.profile
Executed Evertime a Shell is Started
Borne C Shell Korn
N/A ~/.cshrc File specified in .profile with:
ENV variable
ENV=$HOME/.kshrc

Return to the top


Special Variables


A special parameter is a parameter denoted by one of the following special characters. The value of the parameter is listed next to its character.
SH CSH KSH BASH * Expands to the positional parameters, starting from one. When the expansion occurs within a double-quoted string it expands to a single field with the value of each parameter separated by the first character of the IFS variable, or by a if IFS is unset.
SH KSH BASH @ Expands to the positional parameters, starting from one. When the expansion occurs within double-quotes, each positional parameter expands as a separate argument. If there are no positional parameters, the expansion of @ generates zero arguments, even when @ is double-quoted. What this basically means, for example, is if $1 is ``abc'' and $2 is ``def ghi'', then "$@" expands to the two arguments:

"abc" "def ghi"
SH KSH BASH # Expands to the number of positional parameters.
SH KSH BASH ? Expands to the exit status of the most recent pipeline.
SH KSH BASH - (hyphen) Expands to the current option flags (the single-letter option names concatenated into a string) as specified on invocation, by the set built-in command, or implicitly by the shell.
SH CSH KSH BASH $ Expands to the process ID of the invoked shell. A subshell retains the same value of $ as its parent.
SH KSH BASH ! Expands to the process ID of the most recent background command executed from the current shell. For a pipeline, the process ID is that of the last command in the pipeline.
SH CSH KSH BASH 0 (zero) Expands to the name of the shell or shell script.
SH CSH KSH BASH 1 (-9) Expands the first (through ninth) parameter provided, if any.
CSH 10 (-19) Expands the tenth (through ninteenth) parameter provided, if any.
CSH KSH BASH _ (Underscore) At shell startup, set to the absolute file name of the shell or shell script being executed as passed in the argument list. Subsequently, expands to the last argument to the previous command, after expansion. Also set to the full file name of each command executed and placed in the environment exported to that command. When checking mail, this parameter holds the name of the mail file currently being checked.


Page produced by Lagmonster - Aug 2001