' (Single Quote) | $ grep 'Susan Goldberg' phonebook Susan Goldberg 338-7776 $ When the shell sees the first single quote, it ignores any otherwise special characters that follow until it sees the closing quote. $ echo * (list of file names in the current directory) $echo '*' * $echo '< > | ; ( ) { } >> " ` &' < > | ; ( ) { } >> " ` & $ text='*' $ echo $text (Print the list of the filenames in the current directory because * was replaced by the names of all files in the current directory) |
" (Double Quote) | Double
Quote tells the shell to ignore most of the special characters except: |
` (Back Quote) |
$ now=`date` $ echo $now Tue Jul 16 09:34:40 EDT 1990 $ mail `sort -u names` < memo.txt $filename=/user/che/MyFile $firstChar=`echo $filename | cut -c1` echo $firstChar / |
$$ |
$$ shell variable contains the process id number of the current process. |
$# | The number of arguments that ware typed on the commend line |
$* | References all the arguments passed to the program The shell replaces the value of $* with $1, $2, .... |
$@ | References all the arguments passed to the program The shell replaces the value of "$@" with "$1", "$2", .... for arg in "$@" |
$? | Exit status of the last commend executed. |
$0 |
$0 stores the name of the program. |
$1, $2, ... $9 as well as shift |
Use shift when more then 9 arguments passed in. |
${para:-val} |
${parameter:-value} Substitute the value of parameter if it is null. |
&& || |
Constructors
that enable you to execute a command based upon whether the preceeding
commond succeeds or fail sort datafile > /tmp/out && mv /tmp/out mydata grep "$name" phonebook || echo "Couldn't find $name" who | grep "^$name " > /dev/null && echo "$name is logged on" \ || echo "$name is not logged on" |
(...) |
To group a set of commands together: |
awk | awk '/myString/' filename # Print the lines has a match of MyString. awk '{ print $1, $2 }' filename # Print 1st and 2nd field of the file. $0 for the whole line awk -F: '/myString/ { print $1 }' filename # Change the field delimitor to ':' and print the first field of the lines that contain "myString" awk -F"\t" '{ print $2 }' filename # Change the field delimitor to tab. awk -f scriptfile high=100 low=60 datafile awk '{ printf("%d\t%s\n", $5, $9) }' filename $ echo a b c d | awk 'BEGIN { one = 1; two = 2 } > { print $(one + two) }' c /^$/ - Matches blank line. /^.*$/ - Matches entire line. |
basename |
filepathname=/usr/bin/troff |
case |
case "$1" in 0) echo zero;; 1) echo one;; 2) echo three;; *) echo other;; esac hour=`date +%H` case "$hour" in 0? | 1[01] ) echo "Good morning";; 1[2-7] ) echo "Good afternoon";; * ) echo "Good evening";; esac |
cut | cut -c2-6 filename cut -ddchar -ffields filename eg. -f1,2,3 or -f2-4 or -f3- |
date |
date +%H get the
hour date +%M get the minute date +%S get the second date +%Y get the year (YYYY) date +%y get the year (yy) date +%m get the month date +%d get the day |
dd |
dd if=/dev/zero of=myFile bs=512 seek=10241 count=51200 conv=notrunc |
echo |
echo always automatically displays a terminating newline character after the last argument. To suppress it, put the escape characters at the end: |
eval | When putting a eval in front of a command, it cause the shell to scan the command line twice before execute it. |
exec |
Replace the current program with the new one. Any command following the exec command in the script is not executed. |
for |
for i in 1 2 3 do ... done for file in memo1 memo2 memo3 memo4 do run $file done # Entire loop can be sent to the background: for file in memo[1-4] do run $file done & for file in `cat filelist` do run $file done Use break to exit from the loop. Use break n to exit the n intermost loops. |
grep | grep '[Tt]he' filename grep -v 'string1' filename # Lines do not contain string1 grep -vE "string1|string2" filename # Lines do not contain string1 or string2 grep -l 'string' * # List file name only. grep -n 'string' filename # List Line number. |
if |
if ... then ... else ... fi if ... then ... elif ... then ... fi |
IFS | IFS - Internal Field Separator. Contain a space, a tab and two '\n's. |
LANG | export LANG=en_US< /FONT> |
ln | ln -s /SourceDir/SourceFileName /LinkDir/CreatedLinkName |
mailx | mailx -s <subject> <to> <<EOF Your message goes here... EOF or cat <Filename> | mailx -s <subject> <to>< /FONT> |
PS1 |
PS1 - The characters that the shell displays as your command prompt are stored in the variable PS1. Can be changed. |
read |
Read a line from standard input, store the first word in the variable x and the remainder of the line in the variable y: |
readonly | To specify variables whose values cannot be subsequently changes: |
sed | sed command file sed 's/Unix/UNIX/g' filename who | sed 's/ .*$//' # Delete all from the first blank. sed '1,2d' filename # Delete 1st and 2nd line. sed '2,/^$/d' filename # Delete line 2 through the end of file. sed '/[Tt]est/d' filename sed '/UNIX/!d' filename # Delete lines not contain "UNIX" sed -n '1,2p' filename # Print only the 1st and 2nd line. sed -n '/UNIX/p' filename # Print the lines contain "UNIX" sed 's/...$/d' filenamei> # Delete last three charactors for each line. sed '1,10s/unix/UNIX/g' filename # Change unix to UNIX whereever it appears in the first 10 lines. sed -f scriptname file[s] |
set unset |
List all the variables exist in the environment using set with no arguments: |
sort | -u Unique -r Reverse -o Output to file -n Sort arithmetically +1 Sort past the first field -t Delimiter -k Field[s] sort [-u] -k3,3 -t: filename |
tr | tr fromChar toChar cat filename | tr '-' '\12' tr '[a-z]' '[A-Z]' < filename tr ' ' ' ' < filename # Squeeze option turn multi-occurrences of spaces to one space. tr -d '\14' # Delete all formfeed (octal 14). tr -d '[0-9]' # Delete all digits |
uniq | uniq inFile
outFile uniq filename # Print (consecutive) unique lines sort filename | uniq -d # Print the duplicate line in file. sort filename | uniq -c # Print count of occur and the line. |
until |
until who | grep "^$user " > /dev/null do sleep 60 done echo "$user has logged on" Use break to exit from the loop |
while |
while [ "$#" -ne 0 ] do echo "$1" shift done i=1 while [ "$i" -le 5 ] do echo $i i=`expr $i + 1` done Use break to exit from the loop |
test
string operator: |
string1 = string2 string1 is identical to string2 string1 != string2 string1 is not identical to string2 string string is not null -n string string is not null ( and string must be seen by test) -z string string is null ( and string must be seen by test ) $ test -z "$var" $ [ -z "$var" ] $ echo $? if [ -z "$var" ] then echo "var is null" fi |
test
integer
operators |
int1 -eq int2 int1 -ge int2 int1 -gt int2 int1 -le int2 int1 -lt int2 int1 -ne int2 if [ "$count" -eq 0 ] then ... fi |
test file operator | -d file file is a directory -f file file is an ordinary file -r file file is readable by the process -s file file has nonzero length -w file file is writable by the process -x file file is executable [ ! -f "$myfile" ] |
-a -o |
Logical AND/OR operators [ -f "$mailfile" -a -r "$mailfile" ] [ "$count" -ge 0 -a "$count" -lt 10 ] use parentheses to alter the order of evaluation, and parentheses are quoted since they have special meaning to the shell. "$a" -eq 0 -o \( "$b" -eq 2 -a "$c" -eq 10 \) |