I've run across a strange difference in behaviour between bash and dash, where quoted command substitution has some differences with newlines.
I'm not sure if this behaviour is intentional or not, but I suspect it's not because both bash and dash are posix compliant and this seems fairly core behaviour.
To illustrate, first I create a data file:
$ echo '"\n"' > data.txt
To confirm what bytes are in the file, I can run:
$ xxd data.dat
00000000: 225c 6e22 0a "\n".
Then, I run the following bash script:
#!/bin/bash
data=$(cat data.txt)
echo "$data"
The output is:
$ ./test.bash
"\n"
Then I run the following dash script. It's the same as the bash script, but runs in dash instead of bash:
#!/bin/dash
data=$(cat data.txt)
echo "$data"
The output is:
$ ./test.dash
"
"
I thought that the behaviour should be the same between the two different shells, but it's not. Does anyone know why the behaviour differs?
The version of dash I'm running is:
$ dpkg -s dash
Package: dash
Essential: yes
Status: install ok installed
Priority: required
Section: shells
Installed-Size: 199
Maintainer: Andrej Shadura <andrewsh@debian.org>
Architecture: arm64
Multi-Arch: foreign
Version: 0.5.12-2
Depends: debianutils (>= 5.6-0.1), dpkg (>= 1.19.1)
Pre-Depends: libc6 (>= 2.34)
Description: POSIX-compliant shell
The Debian Almquist Shell (dash) is a POSIX-compliant shell derived
from ash.
.
Since it executes scripts faster than bash, and has fewer library
dependencies (making it more robust against software or hardware
failures), it is used as the default system shell on Debian systems.
Homepage: http://gondor.apana.org.au/~herbert/dash/
The version of bash I'm running is:
$ bash --version
GNU bash, version 5.2.15(1)-release (aarch64-unknown-linux-gnu)
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
This happen because echo
in build-in command in the shell. So different shells have different implementation of same command. Better use printf
where you define exactly what and how you want to see the things.
Or as suggested in comment use OS executable:
/usr/bin/echo.....