Difference between single (' ') quote vs double (" ") quote
Single Quote
Using single quote in bash script will preserve the literal value of each characters in the single quotes. It will not do any variable interpolation but instead treat it as it is. Basically what you entered is what you will see. None of that escape character bullshit either. If you enter '\n'
you will see "\\n"
in your program since it preserve the literal value!
echo -E '"\hello!\n"'
# In the program the argument will be ["\"\\hello!\\n\""]
However, the downside of using this is that you cannot put a single quote literal in between. Even if you use backslash! It will not work.
Workaround with ' using ANSI-C Quoting
If you want to use single quotes in the parameter you are passing then you might want to use the $''
ANSI-C quoting for parameter passing.
With $''
you can pass a string with single quotes in them like so:
echo -E $'\'helloworld\\n'
However, then you will lose the literal preserving property of single quotes and then you will have to escape single quotes, backslashes and other special characters, accordingly because they will be treated as special characters if escaped!
You cannot do variable interpolation in ANSI-C Quoting! If you want to do variable interpolation use double quotes.
Double Quote
Using double quote it will preserve the literal value as wellwell, except $, `, \ which will expand those variables or commands.
Backslashes will escape the following special characters and will be removed: $, ", ` (backtik), \, <newline>
echo -E "\$\`$""
# In the program the argument is ["$`\""] because dollar sign, backtik have no special meaning in programs
# they are treated as string literals.
# but the double quote have to be escaped because double quote have special meaning in programs.
Otherwise, if you backslash a non special character, the backslash will stay there as well as the non special character.
Aside: Passing \n as Bash Argument
To pass the actual newline character not the literal in Bash you can do it in the following ways.
./myProgram "
"
OR
./myProgram $'\n'
Do not try to pass it via ./myProgram "\
this will escape the backslash so your actual string that gets passed is "\\n"n'\n"., a length of 2!
Aside: zsh echo
The zsh version of the echo will automatically append the -e
flag which enable interpretation of the escape characters. Which mean the escape character will be escape again after you pass it in! Which often conflict with the examples provided in this chapter. So to prevent it either use bash
or append the -E
flag to prevent interpretation of the escape character again.