Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 08-03-2009, 02:23 PM   #1
LQ Newbie
Registered: Aug 2009
Posts: 2

Rep: Reputation: 0
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 and then after running the C program, I issue:

$ ./
It prints 0. contains nothing but the echo $? command only.
Can anybody help me please?
Old 08-03-2009, 03:16 PM   #2
Registered: Jul 2009
Location: Montréal
Distribution: Slackware customized
Posts: 48

Rep: Reputation: 11

'$?' 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?


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
echo and cat command rdpatel55 Linux - General 0 05-26-2009 06:42 AM
more information about echo command shuchi Linux - Newbie 9 04-18-2009 07:15 AM
echo command s_hcl Linux - General 1 09-06-2006 06:27 AM
echo command peculiarity eallen Linux - General 3 02-11-2003 06:30 PM
Echo /devPrinting doesn't work, echo /usb/lp0 works, Testpage works, Printing doesn't Hegemon Linux - General 3 08-15-2002 02:13 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:47 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration