Technology and General Blog

The Linux established command is a built-in shell command that makes it possible for you to display screen or established both equally shell and environment variables. In this guide, we address the established command and show the different ways that the command-line utility can be utilised.

Essential Syntax

The established command normally takes the following syntax:

$ command -choices arguments

Command Possibilities

There are rather a number of alternatives that can be used with the set command. Let’s discover some of them:

  • -a:   The -a possibility sets all made or modified variables or features for export.
  • -b:   The -b option quickly alerts the person when the history work opportunities are terminated.
  • -e: The -e choice instructs a shell to exit if a command yields a non-zero exit position. Simply just put, the shell exits when the command fails.
  • -f: The -f solution disables the era of filenames.
  • -h: The -h choice is enabled by default. It locates and then remembers a perform as it is awaiting execution.
  • -n: The -n option only reads commands but fails to execute them.
  • -t: The selection -t exits on looking through and managing a single command.
  • -u: The -u option treats unset or undefined variables except for exclusive parameters these kinds of as wildcards (*) or “@” as errors all through parameter enlargement.
  • -v: The -v choice prints out the strains of the shell input as they are becoming study.
  • -x: The -x possibility prints command arguments throughout execution

Exit Values

The subsequent are the shell exit values related with the established command:

: Command was profitable.

  1.  Command unsuccessful due to an incorrect command argument
  2. Command failure because of to an expected argument that is lacking

Set Command Without the need of Any Choices

Devoid of any arguments, the established command lists all the shell variables, together with their values.

Established Positional Parameters With the Established Command

The Linux set command can be utilised to assign values to positional parameters. A positional parameter is a variable in a shell software, and its value is referenced as $N in which N is a digit denoting the position of the parameter.

The $1 price is the 1st positional parameter following the title of the file or command. The $2 worth is the next parameter, and so on.

Suppose we execute the command demonstrated under:

Listed here, crimson corresponds to positional parameter $1, blue corresponds to parameter $2, and eventually, eco-friendly corresponds to  $3.

To record all the parameters in the purchase of $1 $2 $3 operate the echo command beneath:

To listing the initial parameter, execute:

To checklist the 2nd parameter, operate:

And so on.

Use Set Command to Unset All Positional Parameters

To unset the positional parameters run the set command with double hyphens — as revealed.

At the time all over again, if you test to list the positional parameters, you will get blank output, implying that they have been unset.

Dismiss An Unbound Variable

By default, a shell script overlooks an undefined variable. In the script proven beneath, the $foo variable is not but defined and consequently, doesn’t exist.

When the script is run, it returns a blank line for the line that consists of a non-existent variable and proceeds to execute the pursuing line:

This anomaly is undesired, and builders would want to be notified in case of undefined variables. The established -u directive at the start of the script will print out an mistake on the shell if the script runs into an undefined variable.

When the script is operate but once again, the error about an unbound variable is displayed.

Show an Mistake If a Command Is Non-existent

Commonly, if a command operates into an error and fails to execute, the bash shell will continue to execute the remaining commands. Acquire, for instance, the shell script underneath:

The command foobar is non-existent, and an error must be displayed on the bash shell when the script is executed to present that the script into a problem. Having said that, this does not occur and the shell goes together to execute the up coming line as shown:

Like the past example, this is not fantastic follow when creating shell scripts, primarily for protection and debugging. Preferably, the script need to halt when it encounters an mistake.  To address this scenario, determine the directive set -e at the start of the script as proven.

When you test to run the script yet again, you will run into the error as proven:

Display an Error in Piped Instructions

The directive established -e does not get the job done when working with piped instructions. Look at the script underneath:

When you operate the script, it returns an mistake but proceeds to operate the subsequent command:

To conquer this hurdle, pass the established -eo pipefail directive as shown:

This time all-around, the script terminates and does not execute the future line.

Define Allexport and Notify Options

To set allexport and notify selections, operate the command:

$ established -o allexport -o notify


Those people have been a couple illustrations of how you can use the established command in your shell scripts. As observed, the established command can be a helpful instrument in placing positional parameters and debugging your shell scripts.

Leave a Reply

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