tommy__123__
tommy__123__

Reputation: 57

Different output from $? running under bash -c

Why do the following commands produce different output?

false ; echo $?
output: 1
bash -c "false ; echo $?"
output: 0

Both echo $SHELL and bash -c "echo $SHELL" return /bin/bash so I am not sure why the commands would differ in output.

Upvotes: 0

Views: 90

Answers (1)

Barmar
Barmar

Reputation: 781814

Since the argument to bash -c is in double quotes, the original shell performs variable substitution in it. So you're actually executing

bash -c "false; echo 0"

Change it to single quotes and you'll get the output you expect.

bash -c 'false; echo $?'

See Difference between single and double quotes in Bash

Upvotes: 1

Related Questions