0

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.
Peter Stace
  • 1,384
  • 1
  • 7
  • 6
  • Read [this answer to *How can I 'echo' out things without a newline?*](https://stackoverflow.com/a/38021361/1765658) – F. Hauri - Give Up GitHub Jul 28 '23 at 06:55
  • No, this is not a duplicate of that question; voted to reopen. This is a question about why one shell is interpreting `\n` sequences in echo arguments while another does not. [This is an optional behavior in POSIX; an implementations which claim to be XSI-conformant have to implement these C-like escape sequences in echo and are also forbidden from treating `-n` specially.](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html) – Kaz Jul 28 '23 at 15:20
  • @Kaz Yea, and the correct answer is ***Do not use `echò` for this! Prefer `printf` command!*** And the reason is clearly explained in answer I pointed to. – F. Hauri - Give Up GitHub Jul 28 '23 at 22:49
  • @F.Hauri-GiveUpGitHub Problem is, the accepted answer to the linked question recommends `echo -n` (not `printf`), for the purpose of eliminating the newline, which is not the question here at all. – Kaz Jul 29 '23 at 01:38
  • 2
    @Kaz No: Accapted answer clearly explain: ***This is not portable among various implementations of echo builtin/external executable. The portable way would be to use printf instead:*** – F. Hauri - Give Up GitHub Jul 29 '23 at 08:27
  • @Kaz If you're not convinced, read this [answer to *Echo newline in Bash prints literal \n*](https://stackoverflow.com/a/8467449/1765658) – F. Hauri - Give Up GitHub Jul 29 '23 at 08:56
  • Please [edit] your question and review the tags you applied. Read their descriptions! – Ulrich Eckhardt Jul 29 '23 at 10:01

1 Answers1

2

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.....
Romeo Ninov
  • 6,538
  • 1
  • 22
  • 31