LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 09-09-2005, 08:51 PM   #1
slinky2004
Member
 
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:
Quote:
bash#: rmmod <module>
resource in use
bash#: echo $?
1
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, 09:24 PM   #2
ag2uki
Member
 
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:
Code:
#/usr/bin/bash

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


regards
ag2uki
 
Old 09-09-2005, 09:25 PM   #3
PenguinPwrdBox
Member
 
Registered: Oct 2003
Posts: 568

Rep: Reputation: 31
$? 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 09:53 PM.
 
Old 09-09-2005, 10:34 PM   #4
slinky2004
Member
 
Registered: Oct 2004
Posts: 309

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

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

Rep: Reputation: 52
Quote:
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.

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

Rep: Reputation: 79
$@ 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:
Code:
#!/bin/bash

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

Rep: Reputation: 52
Thanks David!

-twantrd
 
  


Reply


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 01:03 AM
what is "sticky bit mode" , "SUID" , "SGID" augustus123 Linux - General 10 08-03-2012 04:40 AM
Telling people to use "Google," to "RTFM," or "Use the search feature" Ausar General 77 03-21-2010 11:26 AM
"Xlib: extension "XFree86-DRI" missing on display ":0.0"." zaps Linux - Games 9 05-14-2007 03:07 PM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 02:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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