Technology and General Blog

Established –e is utilized inside of the Bash to cease execution instantaneously as a query exits whilst owning a non-zero standing. This function is also used when you have to have to know the mistake place in the managing code. Let us carry on the post to elaborate on the strategy of “set –e” in distinct features of codes.

Set up Bash extensions in Linux. If it is now installed, then you will need to update the model due to the fact the edition will have to be earlier mentioned 4.

Instance 1

In this article, we need a file getting the Bash code. So, produce a file working with a “touch” command by utilizing the Ubuntu terminal. That is written as:

$ contact

We will get two techniques listed here. One particular is to use “set –e” outdoors the operate physique whereas the other is to use it inside the function. Contemplate a file “”. We have to have the file with an extension of .sh as we are producing the code in Bash language. This instance deals with introducing a operate named “helo”.

In this function, we want to print a line so we just made use of the echo command right here to exhibit the concept. As we have declared a functionality below so the perform simply call ought to be required below. So in the conclusion, we have applied the title of the operate as a purpose phone. Here “set –e” is utilized at the start means exterior the system of the operate.

Established –e

Set –e just relate to writing or placing an mistake displaying code.

We have used a straightforward text editor that is existing in the Ubuntu system by default.

$ bash

Now, transferring towards the second technique where by we have to use “set –e” inside the perform. We will see the distinction in between these strategies. Edit the provided code by changing “set –e” from exterior of the functionality entire body to inside of the physique of the “helo” purpose, while the remaining code is the exact same as the preceding a single. You can check out from the impression inserted.

Run the very same command yet again on the terminal to get the outcome.

This suggests that “set –e” results in no change when it is utilized inside or exterior the function physique.

Example 2

This is quite an easy illustration in which following the declaration of bash extension in a file, we have made use of “set –e” in the outer entire body of the function. The “set –e” makes it possible for the terminal to throw an exception in which it finds the error and then the code end execution. Then, the error functionality is declared in this article. The only intent of this perform is to display screen the error concept along with the line range that contains the error.

There is a thing distinctive in this example that is the use of the “trap” keyword. This search phrase makes it possible for the utilization of a constructed-in perform ERR that take the line variety of mistake and then go it to the mistake operate. For this intent of error identification and displaying, we have to have to include a statement or a variable that is not bundled in bash or use some significant line with altering the syntax values.

Entice “Error $LINENOE’ ERR

According to this code, the result ought to present the error at line 10.

Execute the code on the terminal by using the file. You can relate that the file identify is proven with the line amount and an exception is thrown that reveals the command is not found. Next, acquiring a concept of the functionality to depict the line range where by an error has happened.

Illustration 3

This instance specials with making use of two bash documents. One particular is and the other is Take into consideration very first. Right here we have used “set –e” and it is not utilised in the other file. In the same way, in this file only we have utilised the perform get in touch with, whilst the full purpose physique is declared in the other file. We have connected the two the data files by utilizing the key word “source” so that the purpose get in touch with we built below will be able to execute the operate from the other file.

4 Resource “”

Right after that, in the operate connect with, a term is also exhibited.

Echo “Notification: $&#040helo&#041

“Helo” is the title of the functionality.

Now, take into consideration the other file in this file helo1() purpose is declared. In the functionality physique, we have displayed a message only.

Echo “wait: $1

Moving to the other purpose that is helo(). This is the exact same purpose that was identified as by the title in the initial file we have talked about. Within this perform, we have yet again utilized a function contact of helo1(). This is declared previously mentioned the current perform in the very same file so we do not require to hyperlink both equally files by utilizing the “source” search term. With the function call, a message is exhibited:

Helo1 “there exists an error”

The complete performance is performed in this sort of a way that we will run the in the terminal. So, the initially purpose get in touch with will be executed and the management will go to the helo() function in That will execute this purpose connect with and now the manage will move in direction of the to start with perform of the file. Let us see how the output is displayed.

$ bash

Now you can see that first the term is displayed from the and then the information of operate “helo1()” that is “wait” and then the concept of perform helo(). As we have employed “exit 1”, the management is not presented to that so there is no role of “set –e” once more. If the function get in touch with is not handled, there will have to be an mistake to prevail.

Example 4

This illustration includes a entire being familiar with of “set –e”. Choose four capabilities in this instance. As we know that the established –e built-in is utilised to exit the code when it gets non-zero status. In this instance, we have utilised “0” for only a single operate that is the first one particular. All other functions return 1. This usually means that the code will exit execution following the initial operate is exhibited. But it will not. Listed here, we have used “set +e” which is the reverse of “set –e”. Any time “set –e” pressured the code to terminate execution, the opposite 1 will oppose it every time it encounters the non-zero benefit. “set +e” is declared in advance of the functionality call of the 1st two functions and “set –e” right before the operate connect with of the final two procedures.

Established +e

Now, initial two features will be executed. In the 2nd functionality, as it is a non-zero worth, the compiler will power to throw an mistake but “set +e” will neutralize the benefit. When it’s the time for the 3rd operate, the two the messages will be shown by echo but when the regulate goes to the “return 1” value, then the code will quit. As right here, we did not use “set +e”. That is why the 4th functionality is not executed in this article.

Execute the code in the terminal so that you will see the resultant worth.


This tutorial exhibits the doing work of “set –e”. In the illustrations, which is how it is employed to terminate the execution. Even so, the opponent “set +e” is also used in this article to illustrate the working.

Leave a Reply

Your email address will not be published. Required fields are marked *