Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set -e and set -x in shell script

What is the difference in giving

set -e set -x

and

set -xe

I'm getting different responses for these. please help me in to get clarified

like image 968
Bakkya Avatar asked Mar 19 '15 09:03

Bakkya


People also ask

What is set in shell script?

The set command is a built-in Linux shell command that displays and sets the names and values of shell and Linux environment variables. On Unix-like operating systems, the set command functions within the Bourne shell ( sh ), C shell ( csh ), and Korn shell ( ksh ).

What is set +E in Bash?

Set –e is used within the Bash to stop execution instantly as a query exits while having a non-zero status. This function is also used when you need to know the error location in the running code.

What happens if you use set in a bash script?

In Bash, the set command allows you to manage certain flags and characteristics to influence how your bash scripts behave. These controls ensure that your scripts follow the correct path and that Bash's peculiar behavior does not cause difficulties.

What does set mean in script?

set is a command that allows you to enable certain flags in your Bash script to make the script follow certain behaviors and characteristics. If we take a look at the man page, the description is as follows: Set or unset values of shell options and positional parameters.


2 Answers

set -x

Print shell command before execute it. This feature help programmers to track their shell script.

set -e

If the return code of one command is not 0 and the caller does not check it, the shell script will exit. This feature make shell script robust.

set -e and set -x often appear at the head of shell script:

set -x
set -e

echo "I am a shell script."

Or use as shell command:

sh -xe shell_script.sh

Reference: http://julio.meroh.net/2010/01/set-e-and-set-x.html

like image 66
g10guang Avatar answered Nov 02 '22 20:11

g10guang


Presumably you are talking about bash. According to its manual, -e (see section 4.3.1) tells bash to exit under certain conditions (which may apply to your script). If that is the case, "set -xe" might print more of the trace before exiting than "set -e" followed by "set -x".

like image 41
Thomas Dickey Avatar answered Nov 02 '22 21:11

Thomas Dickey