why \\\\ and \\\\\ in printf format result in same output?
Code:
debian8@debian:~$ printf %-5s\\\\n "hah" Please explain it in detail. |
Homework question?
|
i don't think so.
this user has a different posting history. and i have been asking myself the same / a similar question sometimes, but with php. in my shell bash i get very different behavior: Code:
$> printf '\\\n' |
Well, 'quotes' do "matter". It isn't hard to test the possible combinations:
Code:
echo a\\b |
ok, let's have a try: what will happen when you press enter? First the current bash will try to evaluate the command line you entered.
Code:
printf is a (built-in) command, which will be executed Code:
printf is a (built-in) command, which will be executed Code:
printf |
I would recommend to start to read about the original C language, first. Then, you can see why \\ or why \n. There is something important with '\', i.e. how your linux and windows 7,8,10 works.
|
Different languages and contexts have different escape characters and sequences. The '\' has special meaning in C, PHP, the shell and a good many other languages, although the rules are not always the same (as we will see below!). The question in this thread is specifically about the shell, and about printf in shell context which adds its own rules for '\'.
I usually think that people who seek out edge cases and obscure command superpositions really have too much time on their hands! ;) There are also differences in printf usage among shells, so we will restrict what follows to Bash which uses a built-in printf. The obscurity of the case posted here is caused by the fact that the backslashes are handled twice, with different rules! First, because the format string is not quoted, the shell applies its rules for special meaning of the backslash: Code:
\\=\ Code:
\\=\ As noted above, quoting is important, and I can hardly think of a useful case where you would not want to quote a printf format string! But there are still two choices... single quotes and double quotes. In this case the single quoted string is usually the winner, it protects the format string from shell interpretation and tells printf what you really mean! Now, all that said, it is interesting to compare the different patterns that result from unquoted, single quoted and double quoted format strings. Following are examples of one through eight backslashes followed by 'n', in each case, using the OP's printf specifiers. Unquoted format string - slashes escaped by shell, \\=\, \n=n: Code:
xxx=string, (nl)=newline Printf sees: Result: Code:
xxx=string, (nl)=newline Printf sees: Result: Double quoted format string - slashes escaped by quoting, \\=\, \n=\ followed by n Code:
xxx=string, (nl)=newline Printf sees: Result: The lesson to be learned: Always single quote printf format strings in shell scripts, and use the time you save for better purposes! |
additionally you can use set -xv to see what's happening (for example):
Code:
user@host:~$ set -xv |
Quote:
It comes from earlier A and B languages. |
All times are GMT -5. The time now is 10:40 AM. |