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




No comment present!

Write a comment

You can write here a comment to the article you've just read. Smiles, links and images are not allowed. The maximum comment length is 4000 characters. Please be polite, all the offensive messages will be deleted.

Your comment (lascia bianco!):
Uses (max 25 characters, required)
Web site (max 255 characters, optional)
e-Mail (max 255 characters, optional, will not be published) Your opinion (lascia bianco!):
Comment (max 4000 characters, required):

Valid HTML 4.01 Transitional
E-Mail - 36.36 ms

Valid HTML 4.01 Transitional