Browsing around...
News  News Links  Links Blog  Blog Italiano  Español 
12 - Return values in the bash


After a script or a command ended, it's useful to leave a trace in the system of its execution, in order to allow the following scripts to know if it was successful and to behave consequently. For this purpose this command exists:

exit return_value

It ends the execution of a script and makes return_value available in the operating system environment.

The following script shows an interesting usage of the return values and command line parameters:


# The following script searches for the user "pippo" in the system
# user list through the grep command. According to the return
# value of grep, it visualizes a message telling if the user
# was found or not.

# It searches for the user "pippo" in the list of system users
grep "admin" /etc/passwd

# According to the return value, it prints a message.
# Remember that $? returns the return value of the last
# executed command.
if [ "$?" -eq 0 ];
  echo "User found"
  exit 0
  echo "User not found"
  exit 1



Fatal error: Call to undefined function sqlite_open() in /membri/giacobbe85/include/ on line 324