Programmers Quick Unix shell cross reference guide for portability needs

 
Bourne Shell C Shell
differences
MsDos 
batch
Comment
sh  csh file.bat command
1978 1978 year introduced
#!/bin/sh #!/bin/csh Unix first line
# REM comment but parses line
:: comment
Comment
set -v set verbose verbose on
set +v unset verbose verbose off
set -x set echo @echo on debug mode on
set +x unset echo @echo off debug mode off
$1, $2, ..., $9 %1, %2, ..., %9 command-line arguments
shift shift shift shift args one position by one
$# number of command-line args
$0 name of command
echo "$*"
echo  "$1 $2 $3 ... "
glob all args in one string
echo "$@"
echo "$1" "$2" "$3" ...
give all args as seperate strings
$? $status exit status
if referenced more than once
then must save in variable
V=1 set V 1 set  V=1 variable assignment
no blanks around equal sign
V=$W set V $W set V=%W%
$V %V% Variable Ref
echo $V

echo ${V}

  echo %V% display
V='abc$Wdef' set V='abc$Wdef' literal string
='single quotes'
=no $variable substitution
=no ${varible} substitution
=useful for blanks/spaces
V="abc$Wdef" #error

V="abc${W}def" #correct

non-literal string
="double quotes"
=$variables substitution
=useful for blanks/spaces
V=`expr $N + 1` executable string
=`grave accent quotes'
=useful for arithmetic
=stdout assigned to variable
unset V unsetenv V clear variable
${V} %V% Variable Ref
cmd1 ; cmd2   sequental commands
cmd1 && cmd2   logical AND
cmd1 || cmd2   logical OR
cmd1 | cmd2

cmd1>f1; f1 < cmd2

  pipe
( subshell-cmd )   creates a subshell to run
commands as
a separate process
(cd /tmp;ls);ls   example
{ cmds }   within same shell
brace must be followed by blank
{ date; who }>file.txt    example
echo 'msg=$V'   literal string =
single quotes =
no $variable substitution
program
if [ $? -eq 0 ]; then echo "program ok"; fi
if ERRORLEVEL 0 echo "program ok" testing exit status of 
last program excuted
program
if [ $? -ne 0 ]; then echo "error"; fi;
if NOT ERRORLEVEL 0 echo "error" testing exit status of 
last program executed
if [ -n $V ]; then echo "not empty"; fi
if test -n $V; then echo "not empty"; fi
test -n $V; if [ $? -eq 0 ]; then echo "not empty"; fi
if  NOT %V%=="" echo "not empty" string comparison: non-zero
if [ -z $V ]; then echo "empty"; fi
if test -z $V; then echo "empty"; fi
test -z $V; if [ $? -eq 0 ]; then echo "empty"; fi
if %V%=="" echo "empty" string comparison: zero
if [ -f $V ]; then echo "file exists"; fi
if test -f $V; then echo "file exists"; fi
if exist %V% echo "file exists" file comparison
if [ -d $V ]; then echo "directory exists"; fi
if test -d $V; then echo "directory exists"; fi
file comparison
if [ -d $V ] || [ -f $W ]; then echo "dir or file"; fi
if [ $S = $T ]; then echo "string equal"; fi
if test $S = $T ; then echo "string equal"; fi
if %S% == %T% echo "string equal" string comparison
if [ $S != $T ]; then echo "not equal"; fi
if test $S != $T ; then echo "not equal"; fi
if [ ! $S = $T ]; then echo "not equal"; fi
if test ! $S = $T ; then echo "not equal"; fi
if NOT %S% == %T% echo "not equal" string comparison
if [ $I -eq $N ]; then echo "integers equal"; fi integer comparison
if [ $I -ne $N ]; then echo "integers not equal"; fi integer comparison
if [ $I -lt $N ]; then echo "integer less than"; fi integer comparison
if [ $I -le $N ]; then echo "integer less than or equal"; fi integer comparison
if [ $I -gt $N ]; then echo "integer greater than"; fi integer comparison
if [ $I -ge $N ]; then echo "integer greater than or equal"; fi integer comparison
if  commands; then commands; else commands; fi
for V in 1 2 3 4; do echo $V; done for %%V in ( 1 2 3 4 ) do echo %V
N="1 2 3 4"; for V in $N; do echo $V; done set V="1 2 3 4"
for %%V in ( %N ) do echo %V
N=1; while [ $N -le 4 ]; do echo $N; N=`expr $N + 1`; done
f(){ echo $1; } #define a function f

f abc   #call function f with arg abc

function
date > out; ls >> out; date >> out

 ( date ; ls ; date ) > out

common output