Skip to main content

Backslash Escapes

When you are working with special character arguments for say Python or Bash scripts, remember that if you only put one backslash it will escape the character that comes after it. In the case that the character that comes after it doesn't have any special meaning it will just interpret it as a character literal.

./script.sh \q, then $1 will be the string literal "q" because there is no special escape character with \q.

If you want to pass a backslash, keep in that mind in order to pass the backslash literal to a program you have to type backslash twice to escape the escape character. So \\ -> for one backslash literal interpretation.

In the programming language when you actually print the string it will only be one backslash printed, and the length of that string is only 1!

Exception

The only exception I know so far is \n, if you want to pass \n in command line argument see the following post

Passing \n (not literal)

Otherwise, if you want to pass the string literal "\n" then you would have to pass \\n, the two backslash is interpreted as \ literal.