bash tips needed for understanding how to escape characters in command-line

Posted by Jesper Rønn-Jensen on Stack Overflow See other posts from Stack Overflow or by Jesper Rønn-Jensen
Published on 2010-04-17T19:51:20Z Indexed on 2010/04/17 19:53 UTC
Read the original article Hit count: 411

Filed under:
|

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

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.

© Stack Overflow or respective owner

Related posts about bash

Related posts about escaping