help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Line continuation and pipes within command substitution and process subs


From: Robin A. Meade
Subject: Line continuation and pipes within command substitution and process substitution
Date: Fri, 9 Jul 2021 11:05:37 -1000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0

I ran into an interesting difference in behavior between bash-4.4 and bash 5.0 involving line continuation and pipes within command/process substitutions.

Consider this script:

#!/bin/bash
echo "$(cat <<'EOF' | cat | cat | cat
hello
EOF
)"

It works fine (prints `hello`) on both bash-4.4 and bash 5.0.

Now introduce line continuation:

#!/bin/bash
echo "$(cat <<'EOF' \
| cat \
| cat \
| cat
hello
EOF
)"


Running it on bash-4.4 still gives the expected result:

bash-4.4$ ./myscript.sh
hello


But running it on bash-5.0 gives an unexpected result:

bash-5.0$ ./myscript.sh
./myscript.sh: line 14: warning: here-document at line 12 delimited by end-of-file (wanted `EOF') ./myscript.sh: line 14: warning: here-document at line 12 delimited by end-of-file (wanted `EOF') ./myscript.sh: line 13: warning: here-document at line 11 delimited by end-of-file (wanted `EOF')
hello
EO)


## Workaround: Use curly braces (preferred*) or parentheses to group the pipeline

#!/bin/bash
echo "$({ cat \
| cat \
| cat \
| cat
} <<'EOF'
hello
EOF
)"

*Parentheses would needlessly introduce a subshell.


## This applies to process substitution as well

The following process substitution example works on bash-4.4 but not bash-5.0:

#!/bin/bash
cat < <(
cat <<'EOF' \
| cat \
| cat \
| cat
hello
EOF
)

bash-5.0$ ./myscript.sh
./myscript.sh: line 9: warning: here-document at line 7 delimited by end-of-file (wanted `EOF') ./myscript.sh: line 8: warning: here-document at line 6 delimited by end-of-file (wanted `EOF')
hello
EO

Grouping the commands with curly braces or parentheses again is a workaround:

cat < <(
{ cat \
| cat \
| cat \
| cat
} <<'EOF'
hello
EOF
)


Robin



reply via email to

[Prev in Thread] Current Thread [Next in Thread]