Skip to main content

Programmer's Guide: The "set" command in bash

The "set" command in shell allows you to set various shell options and positional parameters. The purpose of this post is to highlight how much this command has been helpful to me in my bash programming. I would not go into much depth about all the possible flag, because you can find many posts and documentation on the internet, like this post by GNU, which cover all the possible options that the "set" command provides. I will mainly focus on two options,

The "set -x" option: 

This option allows us to print commands and their arguments, after they are expanded (interpolated) but before they are executed.

Let us execute a simple hello world programme in bash.


When the programme is executed, we see the entire printf statement being printed before the actual "Hello World!" message is printed. This way, we can check the command that is being executed along with its output.


Setting the flag also expands any variables or arguments (interpolation) and prints them before execution. Furthermore, we can unset the option using "+" sign. 
In the below example, $1 implies that the programme should print 1st argument provided to it. Also, -x option is unset by setting +x.


If we run the code with 1st argument "Bash", observe that it prints Bash in the print statement before actually printing it. Also observe that the printf command is not shown after unsetting the -x option.

The "set -e" option:

This causes the programme to exit immediately if the command being executed returns a non-zero status. This is useful if you want to prevent further, possibly critical, code execution in case a certain command fails.

Continuing with our programme, I have modified the code to cat (print contents) of a file that doesn't exist. I have also "set -e".


The programme execution stops immediately and does not execute the critical section that comes after.


This flag is also useful in debugging a code. If you want to allow execution of certain commands which may cause errors but not allow certain commands whose output is not known, you can prevent termination of code by creating a complex command which returns status-code 0.

In the below code, we can continue execution in case a certain non-critical file doesn't exist, but we must not execute any further if a critical input file is not present.



As can be observed in the above execution, if a temporary file is missing and if we can safely ignore it, we can use "true" value and OR it with the command output to make a complex command whose exit status code is 0. You can read more about "true" value and exit status codes in bash here.
Status code 0 means that the overall command doesn't have any errors and the programme doesn't terminate. But it still terminates if a critical file is not present. So, execution of critical block is prevented which might have caused major issues.

Both these options have helped me tremendously while writing bash scripts. I hope you will find these options useful. Do explore other set options and let me know their uses in comments below.

Comments

  1. -x & -e are helpful flags while debugging shell scripts
    another approach is to use them in command line "bash -x script_name"

    ReplyDelete

Post a Comment

Popular posts from this blog

Using static IPs in Linux

It is often a requirement to assign static IP addresses to some important and permanent network interfaces. This is to avoid setting up a local DHCP server or relying on IP given by your network provider which may not be stable. This post shows steps to configure static IPs in Linux using Ubuntu 16.04 and Ubuntu 18.04 distros. We are considering two distros as network configuration has changed significantly in Ubuntu 18.04. There are two general steps to be followed: 1. Configure the network interface and assign a static IP. 2. Restart interface for the changes to take effect. Ubuntu 16.04  The current interfaces on the system are as follows: As can be seen, enp0s8 interface does not have any IP. Let's assign a static IP to it. In case you already have an interface with a DHCP assigned IP, you just need to change that IP and make it static. Edit the /etc/network/interfaces file and add the following code block. Prefer using the IP address range available for pri...

Local domain name resolution using /etc/hosts

The /etc/hosts file is a powerful mechanism for managing the information about hosts in the local network in the absence of a local DNS server. Setup We have 4 VMs in the local /24 network. The following are the details: vm-1-ubuntu-16-04 - 10.0.1.11 vm-2-ubuntu-16-04 - 10.0.1.12 vm-1-ubuntu-18-04 - 10.0.1.21 vm-2-ubuntu-18-04 - 10.0.1.22 The VMs are reachable via their IP address but not by their hostnames. This is problematic because we have to remember their IP addresses everytime we want to access these hosts. It is would be much simpler to remember and access the servers by their hostnames. For that we need some sort of mapping between the IP addresses and their corresponding host names. There are 3 common ways of achieving this mapping. Setup a DNS server which handles resolution for your local network. Use an existing DNS server of the local Internet Service Provider (ISP) or any other higher level ISP. Note that a public static IP address is required for this s...

Simple HTTP Server in Python

Have you ever come across a need to share a file over a local network to a number of machines? Have you ever felt the need to share output of a task in text files available at a web-endpoint? Often, there is a requirement to quickly setup a light-weight web-server for such requirements. Python provides the easiest way to setup a simple HTTP server for such use cases with a single command. Python comes pre-installed with Linux as many of the linux libraries use python in background. Therefore there is no extra overhead of installing python. With support for python2 getting stopped in April 2020, many of the newer versions of linux distros come with default python3. Therefore this post will focus on setting up an HTTP server using both python2 and python3. We will be using Ubuntu 16.04, however it will work for majority of the linux systems where python comes pre-installed. Preparation Steps to follow before starting the server: Make sure you are in the directory which you want ...