Quoting characters: Difference between revisions
From WickyWiki
Created page with "Category:Ubuntu Category:Bash Category:201304 When you want to generate bash script using filenames it is important that you quote the filenames correctly. When you ..." |
No edit summary |
||
| Line 12: | Line 12: | ||
# We create a file for testing: '''$dollar `backquote \backslash "dquote 'quote.txt''' | # We create a file for testing: '''$dollar `backquote \backslash "dquote 'quote.txt''' | ||
# We use 'find' to generate the correct cat statement(s) (display contents) for all files ending in quote.txt cat_script.sh | # We use 'find' to generate the correct cat statement(s) (display contents) for all files ending in 'quote.txt' and write these to 'cat_script.sh' | ||
# Make | # Make 'cat_script.sh' executable | ||
# | # Execute 'cat_script.sh' | ||
<syntaxhighlight lang=bash> | <syntaxhighlight lang=bash> | ||
#create filename | #create filename | ||
echo " | echo "testfilename1" > 'test1 $dollar `backquote \backslash "dquote.txt' | ||
echo "testfilename2" > 'test2 $dollar `backquote \backslash "dquote.txt' | |||
#create cat scripts for all files *quote.txt | #create cat scripts for all files *quote.txt | ||
find *quote.txt | sed -e 's/[\\\$\`"]/\\\0/g' | sed 's/\(.*\)/cat "\0"/g' > cat_script.sh | find *quote.txt | sed -e 's/[\\\$\`"]/\\\0/g' | sed 's/\(.*\)/cat "\0"/g' > cat_script.sh | ||
Revision as of 14:54, 6 April 2013
When you want to generate bash script using filenames it is important that you quote the filenames correctly. When you use double quotes you need to escape the following four characters:
- $ (dollar)
- ` (backquote)
- \ (backslash)
- " (double quote)
The following short scripts generates a display script for all filenames that match "*quote.txt":
- We create a file for testing: $dollar `backquote \backslash "dquote 'quote.txt
- We use 'find' to generate the correct cat statement(s) (display contents) for all files ending in 'quote.txt' and write these to 'cat_script.sh'
- Make 'cat_script.sh' executable
- Execute 'cat_script.sh'
#create filename echo "testfilename1" > 'test1 $dollar `backquote \backslash "dquote.txt' echo "testfilename2" > 'test2 $dollar `backquote \backslash "dquote.txt' #create cat scripts for all files *quote.txt find *quote.txt | sed -e 's/[\\\$\`"]/\\\0/g' | sed 's/\(.*\)/cat "\0"/g' > cat_script.sh #execute chmod +x cat_script.sh source cat_script.sh
More info on quoting: