Shell Environment Note:

Conventional UNIX shell is an interpreter and shell script is a macro-expansion based language.  Actually, shell script can be any kind of language.  For example, scsh use 'scheme-48' as its control language.  I remember BASIC is designed to be the OS control language as well.  At least, people should remember Apple BASIC on Apple II.  Of course, we should remember a terrific language, FORTH.  Unfortunately, all the language I mentioned, BASIC, Scheme, FORTH are better languages than shells we used in UNIX environment.  Although conventional UNIX shells like sh, ksh, bash, csh and tcsh are not perfect enough, they bind all UNIX software tools as the power tools with UNIX IPC mechanism.

Input/Ouput Redirection:
 

csh
sh,bash
input from infile
< infile
< infile
output to outfile
> outfile
> outfile
append to outfile
>> outfile
>> outfile
write to noclobber outfile
>! outfile
 
append to noclobber outfile
>>! outfile
 
<< [delimiter string]
<< [delimiter string]
>& outfile
2>&1 outfile
>>& outfile
2>>&1 outfile
|& outfile
2>&1| outfile
 
3<& infile
 
2|
close stdin (fd:0)
 exec <&-
exec <&-
close stdout (fd:1)
 
exec 1>&-
close stderr (fd:2)
 
exec 2<&-
Write to file descriptor 4
 
>&4
 
 
csh sh,bash
if ( ... ) then 
... 
else 
... 
endif
if [ ... ] ; then 
... 
else 
... 
fi
foreach x in ( ... ) 
... 
end
for x ... ; do 
... 
done
while ( ... ) 
... 
end
while ... ; do 
... 
done
until ... ; do 
... 
done



Here is an example, which is OK in most of the case but ...

if    [    $VAR != "Hello World"    ]    ;    then
    echo $VAR
fi

Because 'sh' is a macro expantion language.  If $VAR is not exist, the code will be expanded as

if    [   != "Hello World"    ]    ;    then
    echo $VAR
fi

And the shell will complain `!=' is not a unary operator and return an error.  If we use double quote to quote $VAR, "$VAR" , then the code will be expanded as

if    [    "" != "Hello World"    ]    ;    then
    echo $VAR
fi

Therefore, to write a safe code to operate string, we should remember to add double quotes for variable.



Last updated on 03/09/98
Any comment or suggestion, email tyuan@sinica.edu.tw