LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   what is "$?" (https://www.linuxquestions.org/questions/linux-newbie-8/what-is-%24-361925/)

slinky2004 09-09-2005 08:51 PM

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.

ag2uki 09-09-2005 09:24 PM

"$" 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

PenguinPwrdBox 09-09-2005 09:25 PM

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


slinky2004 09-09-2005 10:34 PM

thanx :D

eddiebaby1023 09-10-2005 09:09 AM

Quote:

Originally posted by ag2uki
In bash shell, "$?" means the arguments that typed after a command.

Please don't post misinformation.

twantrd 09-10-2005 02:21 PM

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

david_ross 09-10-2005 02:52 PM

$@ 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


twantrd 09-11-2005 05:15 AM

Thanks David!

-twantrd


All times are GMT -5. The time now is 10:13 PM.