| Variables : |
| Assigning values to local variables | set x = 5 | x=5 | x=5 | x=5 |
| Assigning variable attributes | | | declare or typeset | typeset |
| Assigning values to environment variables | setenv NAME Bob | NAME='Bob'; export NAME | export NAME='Bob' | export NAME='Bob' |
| Read-Only Variables: | | | |
| Accessing variables | echo $NAME set var = net echo ${var}work network | echo $NAME var=net echo ${var}work network | echo $NAME var=net echo ${var}work network | echo $NAME or print $NAME var=net print ${var}work network |
| Number of characters | echo $%var (tcsh only) | N/A | ${#var} | ${#var} |
| Special Variables: |
| PID of the process | $$ | $$ | $$ | $$ |
| Exit status | $status, $? | $? | $? | $? |
| Last background job | $! (tcsh only) | $! | $! | $! |
| Arrays: |
| Assigning arrays | set x = (a b c) | N/A | y[0]='a'; y[2]='b'; y[2]='c' fruit=(apples pears peaches plums) | y[0]='a'; y[1]='b'; y[2]='c' set A fruit apples pears plums |
| Accessing array elements | echo $x[1] $x[2] | N/A | echo ${y[0]} ${y[1]} | print ${y[0]} ${y[1]} |
| All elements | echo $x or $x[*] | N/A | echo ${y[*]}, ${fruit[0]} | print ${y[*]}, ${fruit[0]} |
| No. of elements | echo $#x | N/A | echo $y{#[*]} | print ${#y[*]} |
| Command Substitution: |
| Assigning output of command to variable | set d = `date` | d=`date` | d=$(date) or d=`date` | d=$(date) or d=`date` |
| Accessing values | echo $d echo $d[1], $d[2], ... echo $#d | echo $d | echo $d | print $d |
| Command Line Arguments (Positional Parameters): |
| Accessing | $argv[1], $argv[2] or , ... | $1, $2 ... $9 | $1, $2, ... ${10} ... | $1, $2, ... ${10} ... |
| Setting positional parameters | N/A | set a b c set `date` echo ... | set a b c set `date` or set $(date) echo ... | set a b c set `date` or set $(date) print ... |
| No. of command line arguments | $#argv $# (tcsh) | $# | $# | $# |
| No. of characters in $arg[number] | $%1, $%2, (tcsh) | N/A | N/A | N/A |
| Metacharacters for Filename Expansion: |
| Matches for: | | | | |
| Single character | ? | ? | ? | ? |
| Zero or more characters | * | * | * | * |
| One character from a set | [abc] | [abc] | [abc] | [abc] |
| One character from a range of characters in a set | [a “c] | [a “c] | [a-c] | [a “c] |
| One character not in the set | N/A (csh) [^abc] (tcsh) | [!abc] | [!abc] | [!abc] |
| ? matches zero or one occurrences of any pattern in the parentheses. The vertical bar represents an OR condition; e.g., either 2 or 9. Matches abc21, abc91, or abc1. | | | abc?(29)1 | abc?(29)1 |
| Filenames not matching a pattern | ^pattern (tcsh) | | | |
| I/O Redirection and Pipes: |
| Command output redirected to a file | cmd > file | cmd > file | cmd > file | cmd > file |
| Command output redirected and appended to a file | cmd >> file | cmd >> file | cmd >> file | cmd >> file |
| Command input redirected from a file | cmd < file | cmd < file | cmd < file | cmd < file |
| Command errors redirected to a file | (cmd > /dev/tty)>&errors | cmd 2>errors | cmd 2> file | cmd 2> errors |
| Output and errors redirected to a file | cmd >& file | cmd > file 2>&1 | cmd >& file or cmd &> file or cmd > file 2>&1 | cmd > file 2>&1 |
| Assign output and ignore noclobber | cmd > file | N/A | cmd > file | cmd > file |
| here document | cmd << EOF input EOF | cmd << EOF input EOF | cmd << EOF input EOF | cmd << EOF input EOF |
| Pipe output of one command to input of another command | cmd cmd | cmd cmd | cmd cmd | cmd cmd |
| Pipe output and error to a command | cmd & cmd | N/A | N/A | (See coprocesses) |
| Coprocess | N/A | N/A | N/A | command & |
| Conditional statement | cmd && cmd cmd cmd | cmd && cmd cmd cmd | cmd && cmd cmd cmd | cmd && cmd cmd cmd |
| Reading from the Keyboard: |
| Read a line of input and store into variable(s) | set var = $< set var = 'line' | read var read var1 var2... | read var read var1 var2... read read -p prompt read -a arrayname | read var read var1 var2... read read var?"Enter value" |
| Arithmetic: |
| Perform calculation | @ var = 5 + 1 | var=`expr 5 + 1` | ((var = 5 + 1)) let var=5+1 | ((var = 5 + 1)) let var=5+1 |
| Tilde Expansion: |
| Represent home directory of user | ~username | N/A | ~username | ~username |
| Represent home directory | ~ | N/A | ~ | ~ |
| Represent present working directory | N/A | N/A | ~+ | ~+ |
| Represent previous working directory | N/A | N/A | ~- | ~ “ |
| Aliases: |
| Create an alias | alias m more | N/A | alias m=more | alias m=more |
| List aliases | alias | | alias, alias -p | alias, alias “t |
| Remove an alias | unalias m | N/A | unalias m | unalias m |
| History: |
| Set history | set history = 25 | N/A | automatic or HISTSIZE=25 | automatic or HISTSIZE=25 |
| Display numbered history list | history | | history, fc -l | history, fc “l |
| Display portion of list selected by number | history 5 | | history 5 | history 5 10 history 5 |
| Re-execute a command | !! (last command) !5 (5th command) !v (last command starting with v) | | !! (last command) !5 (5th command) !v (last command starting with v) | r (last command) r5 (5th command) r v (last command starting with v) |
| Set interactive editor | N/A (csh) bindkey -v or bindkey -e (tcsh) | N/A | set -o vi set -o emacs | set -o vi set -o emacs |
| Signals: |
| Command | onintr | trap | trap | trap |
| Initialization Files: |
| Executed at login | .login | .profile | .bash_profile | .profile |
| Executed every time the shell is invoked | . cshrc | N/A | BASH_ENV=.bashrc (or other filename) (bash 2.x) ENV=.bashrc | ENV=. kshrc (or other filename) |
| Functions: |
| Define a function | N/A | fun() { commands; } | function fun { commands; } | function fun { commands; } |
| Call a function | N/A | fun fun param1 param2 ... | fun fun param1 param2 ... | fun fun param1 param2 ... |
| Programming Constructs: |
| if conditional | if (expression) then commands endif if { (command) } then commands endif | if [ expression ] then commands fi if command then commands fi | if [[ string expression ]] then commands fi if ((numeric expression)) then commands fi | if [[ string expression ]] then commands fi if ((numeric expression)) then commands fi |
| if/else conditional | if (expression) then commands else commands endif | if command then commands else ... fi | if command then commands else ... fi | if command then commands else ... fi |
| if/else/elseif conditional | if (expression) then commands else if (expression) then commands else commands endif | if command then commands elif command then commands else commands fi | if command then commands elif command then commands else commands fi | if command then commands elif command then commands else commands fi |
| goto | goto label ... label: | N/A | N/A | N/A |
| switch and case | switch ("$value") case pattern1: commands breaksw case pattern2: commands breaksw default: commands breaksw endsw | case "$value" in pattern1) commands ;; pattern2) commands ;; *) commands ;; esac | case "$value" in pattern1) commands ;; pattern2) commands ;; *) commands ;; esac | case "$value" in pattern1) commands ;; pattern2) commands ;; *) commands ;; esac |
| Loops: |
| while | while (expression) commands end | while command do command done | while command do command done | while command do commands done |
| for/foreach | foreach var (wordlist) commands end | for var in wordlist do commands done | for var in wordlist do commands done | for var in wordlist do commands done |
| until | N/A | until command do commands done | until command do commands done | until command do commands done |
| repeat | repeat 3 "echo hello" hello hello hello | N/A | N/A | N/A |
| select | N/A | N/A | PS3="Please select a menu item" select var in wordlist do commands done | PS3="Please select a menu item" select var in wordlist do commands done |