Saturday, May 24, 2014

Unix Shell Role of Quoting

1. Backslash escaping
Backslash escaping tells the shell to treat the character literally.

terminal:
First command is trying to list all files whose name starts with "script" and end up with one ANY character.
Second command is trying to list all files whose name starts with "script" and end up with just question mark
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls script?  
 script? script2  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls script\?  
 script?  

2. Single quote
Single quote enforce the shell to treat all characters literally.

terminal:
1) First command list all files whose name starts with "script"
2) Second command list all files whose name starts with "script", and plus ONE ANY character
3) Third command list all files whose name is just "script?". Single quote treat question mark literally
4) Fourth command list all files whose name is just "script*". Unfortunately, no file has such a name
5) Fifth command is tricky. We intends to nest one question mark inside the single quote.('?' is nested in outsider quote). But the shell treat it in different way, it will use first 2 single quote to tackle "script", then question mark is outside any single quotes, represents any single character here, the last two single quotes combine together to represent empty string. So the entire statement means, list all files whose name starts with script, plus any single character and plus empty string.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls script*  
 script? script2  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls script?  
 script? script2  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls 'script?'  
 script?  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls 'script*'  
 ls: cannot access script*: No such file or directory  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls 'script'?''  
 script? script2  

3. Single quotes along with double quotes
terminal:
1) First command, double quote is nested inside the single quotes, and every thing in single quotes is treated literally. So in this case, double quote just get outputted
2) Second command, we intend to escape the last single quote, but unfortunately, the backslash is treated literally inside single quotes
3) Third command, the last escaped single quote is not belonging to any single quotes pair, it is just tell echo to literally treat the single quote, and we just want to output one single quote here. The 2nd single quote in the statement is pairing with the first single quote.
4) Based on third command, we just append 's it going"', which output everything inside these single quotes.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo 'He said:"'  
 He said:"  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo 'He said:"How\'  
 He said:"How\  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo 'He said:"How'\'  
 He said:"How'  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo 'He said:"How'\''s it going"'  
 He said:"How's it going"  

4. Double quotes
Double quotes will not treat every thing inside quotes literally, it will escape the special character like $, `, ", \.

terminal:
1) escape $ to let shell treat $ literally
2) otherwise, $n means empty variable here
3) without backslash to escape `, echo world will be taken as command substitution.
4) using backslash to treat ` literally
5) using backslash to treat " literally
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo "Hello \$n"  
 Hello $n  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo "Hello $n"  
 Hello   
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo "Hello `echo world`"  
 Hello world  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo "Hello \`echo world\`"  
 Hello `echo world`  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo "Hello \"world\""  
 Hello "world"  

No comments:

Post a Comment