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

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -