150 likes | 314 Views
Chapter 6. Dealing with quotes. Dealing with Quotes and slashes. grep Susan phonebook grep Susan Goldberg phonebook results in an error grep ‘Susan Goldberg’ phonebook allows the shell to ignore spaces the single quote ignores the $ sign and * symbol. Double quotes.
E N D
Chapter 6 Dealing with quotes
Dealing with Quotes and slashes • grep Susan phonebook • grep Susan Goldberg phonebook • results in an error • grep ‘Susan Goldberg’ phonebook allows the shell to ignore spaces • the single quote ignores the $ sign and * symbol
Double quotes • Some things in double quotes are NOT ignored – in other words the shell WILL interpret • $ signs • back quotes (on the tilde key) • backslashes • other double quotes • newlines
Examples $ x=* $ echo $x addresses intro lotaspaces nu names $ echo ‘$x’ $x $ echo “$x” *
Hiding quotes with quotes • double quotes hide single quotes and single quotes hide double quotes $ x=“’Good day, ‘ she said” $ echo $x ‘Good day, ‘ she said $ y=‘ “Good quotes are hard to find” ‘ $ echo $y “Good quotes are hard to find”
The Backslash • the backslash serves as a way of putting quotes around the next character $ echo \\ \ $ echo ‘\’ \
The Backslash (cont’d) • The backslash goes as far as causing the shell to ignore a hard return • this is useful for typing in long commands $ lines=command1\ >command2 $ echo $lines command1command2
The Back Quote • this is used to tell the shell to execute the following command $ echo The date and time is `date` The date and time is Wed Jan 24 14:20:34 PST 2007 • be aware that this typically is no longer the preferred method, but you may run across it
The $(…) or command substitution Construct $ echo “The date and time is: $(date)” The date and time is Wed Jan 24 14:20:34 PST 2007 • keep in mind that within single quotes the command substitution construct does not work, but in double quotes it does
Examples • echo There are $(who | wc – l) users logged in • with single quotes • echo ‘$(who | wc –l) tells how many users are logged in’ • with double quotes • echo “You have $(ls | wc – l) files in your directory
more $(…) stuff • to preserve newlines / hard returns use double quotes $ filelist=$(ls) $ echo $filelist – yields everything on one line $ echo “$filelist” – yields the files on separate lines
Nifty stuff • to put the contents of a file into a variable use the cat command $ namelist=$(cat names1) $ echo “$namelist” - will list all the names with newlines • mail $(cat unames) < memo – will email the memo to all users listed in unames who are on your system
Nested commands $ filename=/usrs/steve/memos $ firstchar=$(echo $filename | cut –c1) $ filename=$(echo $filename | tr “$firstchar” “^”) $ echo $filename ^usrs^steve^memos can also be written as
Nested commands (cont’d) $ filename=/usrs/steve/memos $ filename=$(echo $filename | tr “$(echo $filename | cut –c1)” “^”) $ echo $filename
The expr operator • used by older systems – only does integer arithmetic $ expr 1 + 2 3 $ • some quirks • expr 17 * 6 - yields an error • expr “17 * 6” - yields 17 * 6 • expr 17 \* 6 – yields 102