My knowledge of commandline bash is missing on a particular area: I constantly forget how to properly escape characters. Today I wanted to echo this string into a file:
#!/bin/env bash
python -m SimpleHTTPServer
However, this fails:
echo "#!/bin/env bash\npython -m SimpleHTTPServer" > server.sh && chmod +x server.sh
-bash: !/bin/env: event not found
That’s right: Remember to escape ! or bash will think it’s a special bash event command.
But I can’t get the escaping right! \! yields \! in the echoed string, and so does \\!.
Furthermore, \n will not translate to a line break.
Do you have some general tips that makes it easier for me to understand escaping rules?
To be very precise, I’ll accept an answer which tells me which characters I should escape on the bash command line? Including how to correctly output newline and exclamation mark in my example.
Single quotes inhibit all escaping and substitution:
You can alternate or disable quoting if you need to mix things up:
Interpreting escapes is also easy: