0

I was reading about the echo command in UNIX and i found the -e option which is used to enable the interpretation of backslash escapes, the thing is one of these backslash escape sequences was the \ \ ,but when i used the two commands :

echo "hello\\\world"

and

echo -e "hello\\\world"

i get the same output: hello\\world , so what is the difference ? |

user400369
  • 11
  • 1
  • What UNIX system were you reading about, and on what system did you perform your tests? – roaima Mar 15 '20 at 23:07
  • 1
    What _shell_ are you using? If it is the `bash` shell, then I can not reproduce what you are seeing. Are you sure that you used double quotes in _both_ examples? Note that the shell will also interpret the backslash as a quoting characters in front of _some_ characters in a double quoted string (backslash being one of these characters, [see here](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_02_01)). Make sure that you understand this _standard_ behavior before trying to understand the _non-standard_ `-e` option to `echo`. – Kusalananda Mar 15 '20 at 23:19
  • @roaima @ Kusalananda , i'm using centos and yes bash shell – user400369 Mar 15 '20 at 23:27
  • thanks , i will take a look at that – user400369 Mar 15 '20 at 23:29
  • A related and important to read Q&A is https://unix.stackexchange.com/q/65803/5132 . – JdeBP Mar 16 '20 at 09:50

1 Answers1

0

The default is to expand escape sequences, the -e option is non-standard.

If your echo implementation does not include -e in the echo output, it is non-compliant.

The problem here is that there are implementations that only follow the minimal POSIX requirements and ignore the fact that a platform that is allowed to be called UNIX-compatible needs to implement support for the POSIX XSI enhancements.

BTW: bash on MacOS X and Solaris is compiled to always expand escape sequences. Bash in the default compile variant is not POSIX XSI compliant.

POSIX compliant shells like ksh93, bosh or dash expand escape sequences by default as required.

If you like to check the behavior, your test string is not a good idea. A better test would be:

echo '\tfoo'

and to check whether foo is prepended by a tab character in the output.

The reason, why you may had problems is that you used foo\\bar and that the shell treats backslashes in double quoted strings already. If you like to check the echo behavior, you need to use single quoted strings as arguments.

schily
  • 18,806
  • 5
  • 38
  • 60
  • i tested all the other backslash escaped characters like \n,\t,\b and no problem with them , the thing is i don't understand what is the difference in \\ in the case of using -e option or not because i get the same result ! – user400369 Mar 15 '20 at 23:34
  • 1
    See my edited answer, the fact that you used a double quoted string is a problem in this case. – schily Mar 15 '20 at 23:37
  • 1
    yes , i used it again with single quotas and now i have different results when using echo 'hello\\world' i get --> hello\\world and when using -e option i get hello\world now i think this makes sense thanks a lot – user400369 Mar 15 '20 at 23:41