' (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:
1. Dollar Sign: $
2. Back Quote: `
3. Backslashes: \


$ x=*
echo '$x'
$x
echo "$x"
*
$

` (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.
Used for creating unique temporary file names when program is run by more than one person.


grep -v "$name" phonebook > /tmp/phonebook$$

$# 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}
${para:=val}
${para:?val}
${para:+val}

${parameter:-value} Substitute the value of parameter if it is null.
${parameter:=value} Same as above but the shell trys to execute it after assign the value. Use :(the null command) to avoid. : ${PHONEBOOK:=$HOME/phonebook}

${parameter:?value} If the parameter is NOT null, the shell substitutes its value; Otherwise, shell writes value to standard error and exit.
${parameter:+value} Substitute the value if the parameter is NOT 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:
$ (cd bin; ls)
# Execute this in a sub shell.
$ { x=100; }
# Execute this in the current shell.

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
dirname

filepathname=/usr/bin/troff
filename=`basename $filepathname`

filename gets troff.

filepathname=/usr/bin/troff
filedir=`dirname $filepathname`

filedir gets /usr/bin

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
Extend a 5M file: myFile to 30M without truncate.

echo

echo always automatically displays a terminating newline character after the last argument. To suppress it, put the escape characters at the end:

echo "Please enter your name here: \c"

echo escape characters:
==========================================================
\b Backspace
\c The line without a terminating
\f Formfeed
\n Newline
\r Carriage
\t Tab character
\\ Backslash character
\nnn ASCII value - nnn - one to three digit octal number.

eval

When putting a eval in front of a command, it cause the shell to scan the command line twice before execute it.

$ pipe="|"
$ ls $pipe wc -l
| not found
wc not found
-l not found


$ eval ls $pipe wc -l
16
The first time shell scan the command line, it substitutes | as the value of pipe. Then eval cause it to rescan the line, as which point the | is recognized by the shell as the pipe symble.

$ cat last
eval echo \$$#
$ last one two three four # get the last argument

four
$ last * # get the last file in current dir

zoo_report

De-reference:
$ x=100

$ ptrx=x # pointer to x
$ eval echo \$$ptrx
100
$ eval $ptrx=50

$ echo $x
50

exec

Replace the current program with the new one. Any command following the exec command in the script is not executed.
exec myexecutable

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.

$ echo "$IFS" | od -dc
0000000 \t \n \n
040 011 012 012

Can be changed.
$ IFS=:

$ read x y z
123:73:99
$ list="One:Two:Three"

$ for x in $list; do echo $x; done
One
Two
Three

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
PS2

PS1 - The characters that the shell displays as your command prompt are stored in the variable PS1. Can be changed.
PS2 - The secondary command prompt, normally >, is kept in the variable PS2. 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:
read x y

while read n1 n2
expr "$n1" + "$n2"
done

read exit status zero unless an EOF detected on the input. CTRL-D for standard input.

readonly

To specify variables whose values cannot be subsequently changes:
$
readonly PATH HOME

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:
$ set

Use set to assign a value to a positional parameter:
$ set a b c

assign a to $1, b to $2, and c to $3.

set options:
-- Don't treat subsequent args preceded by a - as options
+A arr Assign specified values to array
arr
-A arr Same as +A but unset arr
first.
-a
Automatically export all variables that are subsequently defined or modified.
-e
Exit if any command that gets executed has a nonzero exit status. The ERR trap is executed before exiting.
-f
Disable file name generation
-h
Each commond that is found in the PATH becomes a tracked alias
-k Process argument of the form keyword=value
that appear anywhere on the command line (not just before the commond) and place them in the environment of the command.
-m
Turn on the job monitor.
-n Read commands without executing them ( useful for checking for balanced do...dones, and if...fi
s).
-o m Turn on mode m
. (set -o without argument list the all Korn shell mode).
-p
Turn privileged mode.
-s
Sort the positional parameters
-t
Exit after executing one command.
-u
Issue an error if a variable is referenced without having been assigned a value or if a positional parameter is referenced without having been set.
-v
Print each shell command line as it is read.
-x
Print each commang and its arguments as it is executed, preceded by a +

- enable
+ disable

Use unset to remove the definition of a variable from the environment.

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 \)

::: Made with CoffeeCup : Web Design Software & Website Hosting :::
1