0

We have a script, with a return code. For example

#!/bin/bash
exit 42

which is works fine:

$ ./script ; echo $?
42

but if i go:

$ bash << EOF
./script ; echo $?
EOF
0

so it prints 0, while one would expect it to still print 42

1
  • 1
    You don't have any pipes here, you're using bash to process a heredoc Commented Feb 13, 2017 at 14:47

1 Answer 1

5

Your $? is being expanded before executing the script. If you don't want your variables to expand in the heredoc (not a pipe) put single quotes around the name:

bash <<'EOF'
./script; echo $?
EOF

That wil prevent $? from being expanded while passing the string to the new bash command. It will, instead, be evaluated in the string which is what you seem to be going for.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.