You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Minifying heredoc values in command substitutions produces invalid shell #923
For example, running the following file through shfmt -mn:
#!/bin/sh
a=$(
cat <<EOF
hello
EOF
)
echo "$a"
Produces the following shell:
#!/bin/sh
a=$(cat \
<<EOF)
hello
EOF
echo "$a"
Which can't be ran:
sh: line 4: warning: here-document at line 4 delimited by end-of-file (wanted `EOF')
sh: line 3: warning: here-document at line 3 delimited by end-of-file (wanted `EOF')
sh: line 4: hello: command not found
sh: line 5: EOF: command not found
For some context, I'm using this kind of pattern to assign literal strings to a variable. I can use something like read -r -d '' a <<'EOF' in bash, but afaik assigning cat <<'EOF' is the way to do something like that with posix sh.
Digging through the code I've at least figured a workaround:
#!/bin/sh
a=$(
cat <<EOF
hello
EOF
:
)
echo "$a"
Adding an extra statement after the ending heredoc word allows the formatter to correctly flush the heredoc, I'm guessing because of the new line. Now running shfmt -mn produces:
Activity
andreykaipov commentedon Oct 2, 2022
So this behavior is actually not just with command substitutions, but also exists for heredocs in subshells and in process substitutions too.
andreykaipov commentedon Oct 2, 2022
Digging through the code I've at least figured a workaround:
Adding an extra statement after the ending heredoc word allows the formatter to correctly flush the heredoc, I'm guessing because of the new line. Now running
shfmt -mnproduces:syntax: use fewer newlines in heredocs with minify
syntax: use fewer newlines in heredocs with minify