Skip to contentSkip to conversation

evaluation of env variables in DASH

DISCUSSION Dima Sorkin 3 participants 4 messages Oct 19, 2011

lore
Share & tools
lore
#0 lore raw

Hi.
The following DASH behaviour seems buggy to me

-----------------

$ export A='\n'
$ echo $A

$
-----------------

whereas using BASH would result in printing literally \n .

I.e. presumingly DASH does secondary interpolation of escaped symols in values of environment variables.

Please CC me with replies, I am not in the list.

Thanks, regards,
Dima.

P.S.
Wrom the other hand

$ export B=1
$ export A='$B'
$ echo $A
PRINTS $B
$
#1 lore raw
Quoted 2 lines · Dima Sorkin

Hi.
The following DASH behaviour seems buggy to me

The only bug here is your expectations.

Quoted 5 lines

-----------------

$ export A='\n'
$ echo $A

Passing a literal backslash to echo is non-portable. POSIX even says so. And bash can match dash behavior:

$ (shopt -s xpg_echo; A='\n'; echo -$A-)
-
#2 lore raw
In reply to Dima Sorkin
Quoted 6 lines · Dima Sorkin

The following DASH behaviour seems buggy to me

-----------------

$ export A='\n'
$ echo $A
Quoted 7 lines

$
-----------------

whereas using BASH would result in printing literally \n .

I.e. presumingly DASH does secondary interpolation of escaped symols in values of environment variables.

The echo builtin in dash differs from most other echo utilities on Linux and *BSD in interpreting System V-like backslash escape sequences. This is the "expansion" you are seeing and the same thing can be seen in
echo '\n'
It is documented in dash's manual page.

This also happens in bash if you 'shopt -s xpg_echo'.

This behaviour is permitted by POSIX and required for the XSI option, and is more commonly seen on Solaris or other System V derivatives.

The fix is to use printf(1) instead of echo(1) if there is a possibility the string may start with '-' or contain '\'. In this case,
printf '%s\n' "$A"

This has been asked/reported more frequently and the answer has been that it will not be changed.

Signature · 1 line
Jilles Tjoelker
#3 lore raw
Quoted 2 lines

... The echo builtin in dash ...

What confused me is that I thought that /bin/echo is used, and wrong argument is passed to it. I was not aware it is a builtin.

Thanks for replies, regards,
Dima.