Search code examples
shellunixquoting

Escaping special characters in Unix shell


it is said that " " , ' ' , \ escape the special characters a.k.a their special meaning , but when i do something like this

date +"Today is \%A"

it wont write "Today is %A" but it writes same result as there wasnt "\" = "Today is thurstday". Why is this happening? I thought i understood how escaping characters in unix works but this confuses me quite a lot

Im typing it into terminal im on /bin/bash/ (thats what $SHELL outputs) , the strange thing is , that if \ does not escape char in date , using ssh to connect into server and run the command there works with escaping e.g

ssh name@server.com 'date +"Today is %A"'

Solution

  • Escaping a special character is generally in the shell. For instance, in bash I get:

    Today is \Wednesday
    

    That's because the backslash here is being passed to the date command which is not seeing it as a special character and thus is just printing it out.

    At the bash prompt typing:

    echo $efs
    

    returns a blank line as there is no variable called efs set. However if I type:

    echo \$efs
    

    I get:

    $efs
    

    Which means the special meaning of the dollar sign defining a variable name is taken away and the shell just printed the string out as is.