Bash scripts are simple text files that contain a collection of commands. Bash scripts can help with administrative tasks, task automation, and executing multiple commands. They are used to automate recurring tasks/actions.

Advertisement

We can put all the commands that run on the terminal into a bash script and vice versa. Bash Scripts include imperative programming concepts like loops, conditionals, and functions.

Command-Line Arguments are parameters that are specified with the filename of the bash script at the time of execution. The command-line arguments allow the script to perform dynamic actions based on the input:

How to Pass an Argument to a Shell Script

To pass a parameter to a bash script just write it after the name of the bash script in the terminal:

./my-test-script.sh argument 

How to Pass Multiple Arguments to Shell Script

You can also specify multiple arguments separated by space along with the name of the bash script file:

./my-test-script.sh arg_1 arg_2 arg_3............. arg_n 

We can use the predefined variables to recall these arguments in the bash script. The first argument can be recalled by $1, the second by $2, and so on. The pre-defined variable “$0” refers to the bash script itself. The list of some other important predefined variables is given below:

  • $@ : Values of all arguments
  • $# :Total number of arguments
  • $$ : Process ID of the current shell

Now we will use a bash file named animals.sh as an example.

Now we will run this script in the terminal:

Parse Command Line Arguments in Shell Script

How to Pass an Argument Containing Space

If an argument consists of multiple words that are separated by space then we need to enclose it in single quotes to pass it as a single argument:

How to Pass Arguments with Special Characters

We can also pass special characters as arguments. But they need to be written with backslashes before them.

Passing Arguments Using Flags and Options in Shell Script

Using flags and options is another common way of giving input to a bash script. An option is always followed by a value while flags are not followed by any value.

First, we will make a new bash script that takes two different arguments (options) i.e. -n/--name for name, and -i/--id for an identification number.

In the code given above first, we created a variable that stores all the short and long names of our options. On the second line, we evaluated the ARGS variable. Then we used a while loop to assign a block of code to each option.

In the code above shift is used to skip the values of the variables. The digit that follows the shift defines how many variables are skipped.

Similarly

Now we will add a -v flag which prints out a verbose menu:

Flags are not followed by a colon when they are defined in a bash script because they do not take any argument with them unlike options e.g. the -n option takes a name with it such as Rahul.

Conclusion

A human operator carrying out recurring tasks manually is very inefficient. So such tasks should be automated. Scripts can help us do that.

In this write-up, we have learned how to pass arguments through the command line to a bash script. Command-line arguments help us write dynamic scripts which perform different functions depending on the input.

Moreover, we also discussed different types of arguments. We also touched a bit on predefined variables.

Share.

1 Comment

Leave A Reply

Exit mobile version