340 likes | 479 Views
Introduction to Unix (CA263) Quotes. By Tariq Ibn Aziz. Objectives. In this lecture you will learn about one of the most unique feature of the shell programming: The single quote character ‘ The double quote character “ The backslash character The back quote character `.
E N D
Introduction to Unix (CA263)Quotes By Tariq Ibn Aziz
Objectives • In this lecture you will learn about one of the most unique feature of the shell programming: • The single quote character ‘ • The double quote character “ • The backslash character \ • The back quote character `
The Single Quote • This must occur in pair. • There are several reasons to use single quote in the shell
Character Separated by Space • To keep characters together separated by space $ cat phonebook Alice Chebba 596-2015 Bob Swingle 598-9257 Liz Stachiw 775-2298 Susan Goldberg 338-7776 Susan Topple 243-4932 Tony Iannino 386-1295 $
Example $ grep Susan phonebook Susan Goldberg 338-7776 Susan Topple 243-4932 $ $ grep Susan Goldberg phonebook grep: can’t open Goldberg $ $ grep 'Susan Goldberg' phonebook Susan Goldberg 338-7776 $
Shell preserve spaces between quotes • No matter how many space characters are enclosed between quotes, they are preserved by the shell. • Shell removes the extra space characters from line and passes echo four argument one two three four $ echo one two three four one two three four $ • space characters are preserved in the following example $ echo 'one two three four' one two three four $
Special characters inside the single quotes • All special inside the single quotes are ignored by the shell if they appear inside single quote $ file=/usr/steve/bin/prog1 $ echo $file /usr/steve/bin/prog1 $ echo '$file' $file
Special characters inside the single quotes $ echo * address nu names phonebook stats $ echo '*' * $ echo '< > | ; ( ) { } >> " `&' < > | ; ( ) { } >> " `& $ echo 'How are you today, > John' How are you today, John $
Special characters inside the single quotes $ message='I must say, this sure is fun' $ echo $message I must say, this sure is fun $ text='* means all files' $ echo $text address nu names phonebook stats means all files • * was replaced by the names of all file
The Double Quotes • Double quotes work similarly to single quotes, except they are not quite restrictive. • Single quote tells the shell to ignore all enclosed characters. • Double quote says to ignore most. In particular three characters are not ignored inside double quotes: • Dollar signs • Back quotes • Backslash
Example Double Quote[1] $ x=* $ echo $* address nu names phonebook stats $ echo '$x' $x $ echo "$x" * $
Example Double Quote[2] $ address="11 Driscoll Dr > Brampton, ON, L6Y 3J2" $ echo $address 11 Driscoll Dr Brampton, ON, L6Y 3J2 $ $ echo "$address" 11 Driscoll Dr Brampton, ON, L6Y 3J2 • The new line character is depicted by the characters \n
Example Double Quote[3] $ x="‘Hello,’ he said" $ echo $x ‘Hello,’ he said $ $ article=‘" My Name is," Tariq’ $ echo $article " My Name is," Tariq
The Backslash • Basically, the backslash is equivalent to placing single quotes around a single character, with a few minor exceptions. • The general format is: • \c • Where c is the character you want to quote.
Example[1] $ echo > Syntax error: ‘newline or ;’ unexected • Shell saw > and thought you wanted to redirect echo’s output to a file. $ echo \> > $
Example[2] $ x=* $ echo \$x $x • In this case backslash ignore the $ that followed the backslash and as a result variable substitution was not performed. $ echo \\ \ $ echo '\' \
Using Backslash for Continuing Lines • Single quote tells shell to ignore newline $ lines=one’ > 'two $ echo "$lines" one two • Try this with \ instead. $ lines=one\ > two $ echo "$lines" onetwo • The shell treat \ as a line continuation.
The Backslash inside Double Quotes • If backslash precedes any character inside double quotes, then backslash is ignored by the shell and passed on to the program: $ echo "\$x" $x $ echo "\ is the backslash character" \ is the backslash character $ x=5 $ echo "The value of x is \"$x\"" The value of x is "5"
Lab Exercise • Display the following line: <<< echo $x >>> display the value $x, which is $x $ x=5 <<< echo $x >>> display the value $x, which is 5
The Back Quote • The back quote tells the shell to execute the enclosed command. $ echo The date and time is: `date` The date and time is: Tue Jul 16 09:14:22 EDT 1985 $ echo current dir is: `pwd` current dir is: /usr/steve/bin
The Back Quote • The back quote tells the shell to execute the enclosed command. $ cat nu echo There are `who|wc –l` users logged on $ nu There are 13 users logged on
The Back Quote • The Single quotes protect everything. $ echo ‘`who|wc –l` tell how many users’ `who|wc –l` tell how many users • The Double quotes interpret back quotes. $ echo "You have `ls|wc –l` files" You have 7 files
The Back Quote • Execute date and store the output in now $ now=`date` $ echo $now Tue Jul 16 09:14:22 EDT 1985 $
The Back Quote $ filelist=`ls` $ echo $filelist address nu names phonebook stats $ echo "$filelist" address nu names phonebook stats
The Back Quote • To store a content of a file into the variable, you can use cat: $ namelist=`cat names` $ echo "$names" address nu names phonebook stats
Change the Variable Value[1] • The back quote mechanism can be used to change the value of a variable: $ name="Tariq Aziz" $ name=`echo $names | tr '[a-z]' '[A-Z]'` $ echo $name TARIQ AZIZ
Change the Variable Value[2] • The back quote mechanism can be used to change the value of a variable: $ filename=/usr/steve/memos $ firstchar=`echo $filenames | cut –c1` $ echo $firstchar /
Change the Variable Value[3] • The back quote mechanism can be used to change the value of a variable: $ file=exec.o $ lastchar=`echo $filenames|sed 's/.*\(.\)$/\1/'` $ echo $lastchar o
Arithmetic on Shell Variable • The shell has no concept of performing arithmetic on values stored inside variables. $ i=1 $ i=$i+1 $ echo $i 1+1 $
Arithmetic on Shell Variable • The UNIX program called expr evaluate an expression given to it on the command line. $ expr 1 + 2 3 $ expr 1+2 1+2 $ • Each operator and operand must be separated by space.
Arithmetic on Shell Variable • The usual arithmetic operators are recognized by expr: + for addition, - for subtraction, / for division, * for multiplication, and % for modulus. $ expr 10 + 20 / 2 20 • Each operator and operand must be separated by space. $ expr 1+2 1+2 $
Arithmetic on Shell Variable $ expr 17 * 6 expr: syntax error • What happened here? • The shell saw * and substituted the names of all files in your directory. $ expr "17 * 6" 17 * 6
$ expr 17 \* 6 102 Example $ i=1 $ expr $i + 1 2 Example $ i=1 $ i=`expr $i + 1` $ echo $i 2 Arithmetic on Shell Variable