When I run

curl | tee test.txt; echo ${PIPESTATUS[0]}

I correctly see

curl: try 'curl --help' or 'curl --manual' for more information


But when I try to run exactly same command using '/bin/sh':

sh -c "curl | tee test.txt; echo \${PIPESTATUS[0]}"

I get

curl: try 'curl --help' or 'curl --manual' for more information

sh: 1: Bad substitution

How we can resolve Bad substitution problem, please?


You solve it by not using sh.

The PIPESTATUS variable specifically, and the ${var[idx]} array syntax in general, are features specific to the Bash shell. They do not exist in POSIX sh, and even shells that do have arrays might use a different syntax.

It just happens that some Linux distributions symlink their /bin/sh to Bash. Other distributions, however, symlink it to dash, Debian Almquist Shell. Both are compatible with POSIX sh scripts, but only Bash accepts the ${PIPESTATUS[…]} syntax.

So if you want to use it, run bash -c "…" instead.

| improve this answer | |
  • Because its part of the question to use sh. Better answer bellow, with a hack. Not a better "solution", though (I'll agree that a better solution is to avoid sh) – Dr Beco Jul 11 '15 at 21:47

Workaound for '/bin/sh' or busybox

eval ` { ls /dontexists || echo status="$?"; } | tee /dev/null`
echo "# status=${status}"

Trace :

busybox sh ~/bin/test.sh 
+ status=0
+ ls /dontexists
+ tee /dev/null
ls: /dontexists: No such file or directory
+ echo status=1
+ eval status=1
+ status=1
+ echo # status=1
# status=1
| improve this answer | |

Also 'bash -c' seems to have a problem with it when called from tcsh:

bash -c "curl | tee test.txt; echo \${PIPESTATUS[0]}"

PIPESTATUS: Undefined variable.

This works for me from tcsh:

bash -c 'curl | tee test.txt; echo ${PIPESTATUS[0]}'

curl: try 'curl --help' or 'curl --manual' for more information

GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)
called from tcsh 6.17.06

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.