Reputation: 1
I am trying to get bash printf in doubleqoutes to give me the "right" amount of actually printed backslashes... Who is escaping who ? And why does 2 doublequoted backslashes give me 1 printed backslash exactly as 4 doubleqouted backslashes also gives me 1 printed 1 backslash....? Now 6 doubleqouted backslashes gives me 2 printed backslaches and so does 8 doublquoted backslashes ....?? And so on... So what/who is escaping what/who in the amount of bash doubleqouted backslashes...
povje@povje:~$ which bash
/bin/bash
povje@povje:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.5 LTS
Release: 20.04
Codename: focal
Trying to print doublequoted backslashes with printf in bash:
povje@povje:~$ printf "\\"
\povje@povje:~$ printf "\\\\"
\povje@povje:~$ printf "\\\\\\"
\\povje@povje:~$ printf "\\\\\\\\"
\\povje@povje:~$ printf "\\\\\\\\\\"
\\\povje@povje:~$
so
2doublequoted \ gives me 1 printed
4doubleqouted \ gives me 1 printed
6doubleqouted \ gives me 2 printed
8doubleqouted \ gives me 2 printed
10*doubleqouted \ gives me 3 printed
.
.
so why is every second pair of doubleqouted backslahes ignored ?
Upvotes: 0
Views: 78
Reputation: 241848
Backslashes are special in double quotes in bash. You need to escape (i.e. backslash) a backslash in double quotes to get a literal backslash. Use single quotes and you don't need to double backslashes.
Backslashes are also special in printf. You again need to escape (i.e. backslash) a backslash to get it printed.
But if there's a backslash followed by a non-special character in the argument of printf
, the backslash gets interpreted as if escaped, i.e.
printf '\' # prints \
printf '\\' # prints \ again
printf '\\\' # prints \\
# ...
Upvotes: 1