$(<...) fails with nested quoting.

Skip to first unread message

Steven McBride

Aug 17, 2020, 8:52:46 AM8/17/20
to bug-...@gnu.org, ba...@packages.debian.org
Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64'
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-pc-linux-gnu'
-DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash'
-DSHELL -DHAVE_CONFIG_H   -I.  -I../. -I.././include -I.././lib 
-Wdate-time -D_FORTIFY_SOURCE=2 -g -O2
-fstack-protector-strong -Wformat -Werror=format-security -Wall
-Wno-parentheses -Wno-format-security
uname output: Linux tau5vbub1 4.15.0-112-generic #113-Ubuntu SMP Thu Jul
9 23:41:39 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 4.4
Patch Level: 20
Release Status: release

        'echo "$(<\"filename\")"' fails with No such file or directory
    'echo "$(cat \"filename\")"'  also fails no such file or directory
    'echo "$(<filename)"' is okay.
    'echo $(<"filename")' is okay.
    I think I'm doing the nested double quoting correctly.
    The quoting is because in my original case 'filename' was
    a variable that could contained a filename with spaces in it.
    My execution trace:
      ~ 19>cat fn
      w1 w2
      ~ 20>echo $(<fn)
      w1 w2
      ~ 21>echo $(<"fn")
      w1 w2
      ~ 22>echo "$(<fn)"
      w1 w2
      ~ 23>echo "$(<\"fn\")"
      -bash: "fn": No such file or directory
    Running "strace" shows that quote marks are not removed in
    the failing case:
    echo "$(<fn)":
        [pid  3708] dup2(4, 1)                  = 1
        [pid  3708] close(4)                    = 0
        [pid  3708] close(3)                    = 0
        [pid  3708] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
     >> [pid  3708] openat(AT_FDCWD, "fn", O_RDONLY) = 3
        [pid  3708] read(3, "some text\n", 128) = 10
        [pid  3708] write(1, "some text\n", 10 <unfinished ...>
        [pid  3707] <... read resumed> "some text\n", 128) = 10
        [pid  3708] <... write resumed> )       = 10
    echo "$(<\"fn\")":
        [pid   779] dup2(4, 1)                  = 1
        [pid   779] close(4)                    = 0
        [pid   779] close(3)                    = 0
        [pid   779] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
     >> [pid   779] openat(AT_FDCWD, "\"fn\"", O_RDONLY) = -1 ENOENT
(No such file or di
        [pid   779] openat(AT_FDCWD, "/usr/share/locale/locale.alias",
        [pid   779] fstat(3, {st_mode=S_IFREG|0644, st_size=2995, ...}) = 0
    echo w1 w2 > filename
        echo "$(<\"filename\")"

    [Description of how to fix the problem.  If you don't know a
    fix for the problem, don't include this section.]


Aug 17, 2020, 9:21:01 AM8/17/20
to bug-...@gnu.org
Only in the last case. There is no bug here.

>     The quoting is because in my original case 'filename' was
>     a variable that could contained a filename with spaces in it.


For an explanation, see https://mywiki.wooledge.org/CommandSubstitution.

Kerin Millar

Ilkka Virta

Aug 17, 2020, 10:22:03 AM8/17/20
to Steven McBride, bug-...@gnu.org, ba...@packages.debian.org
On Mon, Aug 17, 2020 at 3:53 PM Steven McBride <zzymc...@gmail.com> wrote:

> 'echo "$(<\"filename\")"' fails with No such file or directory

Quotes inside $() are independent from the ones outside. If you escape
them, you get literal quotes as part of the filename.

$ echo hello > '"filename"'
$ echo "$(<\"filename\")"

$ echo another > 'name with spaces'
$ echo "$(<"name with spaces")"

I've been led to understand this isn't so with backticks, though.
Reply all
Reply to author
0 new messages