linux - why in shell script, $? can't echo twice? -
i have 2 simple scripts:
#!/bin/sh echo "this script return sth" exit 105
and:
#!/bin/sh echo "this script print last script return" echo "first run script" ./this_script_return_sth.sh echo "previous script return value: $?" echo $?
the run result is:
this script print last script return first run script script return sth previous script return value: 105 0
anything did wrong? means if want use it, better first store variable?
$?
expands last statement's return code. so, 0 says last echo
statement (i.e. echo "previous script return value: $?"
) sucessful.
from bash manual:
?
($?) expands exit status of executed foreground pipeline.
if need value in multiple places, can store in variable:
./this_script_return_sth.sh rc=$? echo "previous script return value: $rc" echo $rc
Comments
Post a Comment