Printing the return value using 'echo $?' command
Hello everyone.
I've recently started using Linux (Ubuntu 8.10). My question is: I've written a C program that simply returns 5 from main(). When I issue the echo $? command in the terminal, then as expected, it prints 5. But when I enclose the echo $? in a shell script file named ech.sh and then after running the C program, I issue: $ ./ech.sh It prints 0. ech.sh contains nothing but the echo $? command only. Can anybody help me please? |
Hi,
'$?' is a shell variable of the current shell. Your script most likely starts with the line '#!/bin/sh'. That means that commands within your script operate in a new shell with new shell variables. The new shell inherits no environment variables from the calling shell. Typical workarounds are, 1. Call your script with an argument like 'myscript "$?"' and refer to that argument as '$1' in the script. 2. set an environment variable to "$?" before calling the script and export it as follows, export RETURN_VALUE="$?" # NO SPACE between = and "$?". then refer to $RETURN_VALUE in your script. There are possibly other ways of doing this by selecting appropriate switches when calling the new shell. Did you have something else in mind? |
All times are GMT -5. The time now is 06:39 AM. |