Welcome to the most active Linux Forum on the web.
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 09-09-2005, 09:51 PM   #1
Registered: Oct 2004
Posts: 309

Rep: Reputation: 30
what is "$?"

i was reading this tutorial on using modules and one of the examples did this:
bash#: rmmod <module>
resource in use
bash#: echo $?
what is that? i couldnt search it on google cuz google ignores the $ and ? and i experimented it by myself and i couldnt figure it out.
Old 09-09-2005, 10:24 PM   #2
Registered: Oct 2004
Location: Yogyakarta, Indonesia
Distribution: Mandrake 9.0, 9.1, 9.2, 10.0
Posts: 84

Rep: Reputation: 16
"$" is a character for signing a variable.
In bash shell, "$?" means the arguments that typed after a command. For example:
"$1" will get the first argument in the shell command.
"$2" will get the second...
"$3" will get the third...

To ensure yourself, try to make a file like this:

echo $1
echo $2
then, try to execute the file followed by 2 arguments.

Old 09-09-2005, 10:25 PM   #3
Registered: Oct 2003
Posts: 568

Rep: Reputation: 30
$? is a shell variable that contains the exit code of the last command that you ran. Because your code is 1, that denotes an error. A successful command will exit 0.
Don't let the reply above confuse you. While, in most scripting languages (including bash, which he used in the example) the $1, $2, $3, etc.....are the arguments passed to the bash script - but regardless of the shell - $? is the exit code of the previous command.

Perl, for example, does not use $1....$3 - perl uses the builtin variable @ARGV to contain the arguments.

That is found in the instructions that you were reading so that you could ensure that the module unloaded.

Last edited by PenguinPwrdBox; 09-09-2005 at 10:53 PM.
Old 09-09-2005, 11:34 PM   #4
Registered: Oct 2004
Posts: 309

Original Poster
Rep: Reputation: 30
Old 09-10-2005, 10:09 AM   #5
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Originally posted by ag2uki
In bash shell, "$?" means the arguments that typed after a command.
Please don't post misinformation.
Old 09-10-2005, 03:21 PM   #6
Senior Member
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
In bash shell, "$?" means the arguments that typed after a command.
The number of arguments passed is '$#'.

Also, what is '$@'? I have seen that a couple times and I can't understand the meaning of it.

Old 09-10-2005, 03:52 PM   #7
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 66
$@ should be all of the arguments given to the script. Take a look at the "Special Parameters" section of "man bash". Here's a quick example - just save it and run it:

echo We are running as PID: $$
if [ -z $1 ];then
 echo Not called with arguments, recalling: $0 ARG1 ARG2 ARG3
 $0 ARG1 ARG2 ARG3
 echo Called with $# arguments:
 for arg in $@;do
  echo = $arg
 echo true exited: $?
 echo false exited: $?
Old 09-11-2005, 06:15 AM   #8
Senior Member
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
Thanks David!



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
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 02:03 AM
what is "sticky bit mode" , "SUID" , "SGID" augustus123 Linux - General 10 08-03-2012 05:40 AM
Telling people to use "Google," to "RTFM," or "Use the search feature" Ausar General 77 03-21-2010 12:26 PM
"Xlib: extension "XFree86-DRI" missing on display ":0.0"." zaps Linux - Games 9 05-14-2007 04:07 PM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 03:32 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:26 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