I saw the code written somewhere online, and I wanted to know what exactly does “$?” do/give us.
Googling did not help.
Here’s the code I saw it in:
#!/bin/sh
ping -c 2 localhost
if [ $? != 0 ] ; then
echo "Couldn't ping localhost, weird"
fi
ping -c 2 veryweirdhostname.noend
if [ $? != 0 ] ; then
echo "Surprise, Couldn't ping a very weird hostname.."
fi
echo "The pid of this process is $$"
Taken from: http://efod.se/writings/linuxbook/html/shell-scripts.html
$?is a variable holding the return value of the last command you ran.Example C program (
example.c):Example Bash: