Quoting characters: Difference between revisions

From WickyWiki
No edit summary
m 5 revisions
 
(One intermediate revision by one other user not shown)
Line 26: Line 26:
source cat_script.sh
source cat_script.sh
</syntaxhighlight>
</syntaxhighlight>
The script 'cat_script.sh' will contain:
<blockquote>
<pre>
cat "test1 \$dollar \`backquote \\backslash \"dquote.txt"
cat "test2 \$dollar \`backquote \\backslash \"dquote.txt"
</pre>
</blockquote>


More info on quoting:
More info on quoting:
* http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_03.html
* http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_03.html

Latest revision as of 07:26, 5 July 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":

  1. We create files for testing: ~ $dollar `backquote \backslash "dquote 'quote.txt
  2. 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'
  3. Make 'cat_script.sh' executable
  4. 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

The script 'cat_script.sh' will contain:

cat "test1 \$dollar \`backquote \\backslash \"dquote.txt"
cat "test2 \$dollar \`backquote \\backslash \"dquote.txt"

More info on quoting: