1

I have this simple (even silly :) problem: Inside a bash script, I'm trying to store a command's return code to a variable using S?, so as to use it later in the script, but S? is stored literally as S? .
I run this test bash script:

#!/bin/bash
echo "trying to store this command's return code. it should be 0"
rtrn_code=S?
echo $rtrn_code

but instead of getting 0 (success return code) I get S? :

trying to store this command return code. it should be 0
S?

What am I doing wrong? Any ideas?

Thank you in advance.

2
  • 8
    changue S for $ Commented Apr 30, 2015 at 8:40
  • 3
    Gardro, in bash all variable references begin with the $ symbol. The ? is simply a special variable in bash holding the return value from the previous command. So in order to (de)reference the value it holds you must precede it with the $ sign -- $?. The downvotes (not mine) are simply a reflection that before asking here, you are supposed to use diligence to find the answer yourself. This being a fundamental bash-101 issue -- you are going to receive some... Commented Apr 30, 2015 at 9:27

2 Answers 2

5

It's $? and not S?.

rtrn_code=$?
Sign up to request clarification or add additional context in comments.

Comments

2

The return value of the last executed command can be accessed using $?. Replace S? to $? in your script.

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.