Closed andreykaipov closed 1 year ago
So this behavior is actually not just with command substitutions, but also exists for heredocs in subshells and in process substitutions too.
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:
#!/bin/sh
a=$(cat \
<<EOF
hello
EOF
:)
echo "$a"
For example, running the following file through
shfmt -mn
:Produces the following shell:
Which can't be ran:
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 assigningcat <<'EOF'
is the way to do something like that with posix sh.