How to Use Bash Input Parameter? [5 Practical Cases]

Bash, the popular command line interpreter and scripting language, offers powerful capabilities to customize and control scripts using input parameters. In this article, I’ll explore practical examples to demonstrate how to handle bash input parameter effectively. Whether you’re new to Bash or an experienced user, mastering input parameters can enhance your scripting skills and streamline your workflow. Let’s delve into the world of Bash input parameters and unlock their full potential.

Key Takeaways

  1. Getting familiar with the bash input parameter.
  2. Accessing bash input.
  3. Passing multiple inputs into a bash script.

Special Characters and Bash Input Parameter

Bash programming follows some special characters. Some are listed below.

  • $ → This symbol is used when we assign any value or get any value.
  • $@ → Represents all the command line arguments provided.
  • $# → This counts the total number of passed parameters.

Free Downloads

5 Practical Cases of Input Parameter in Bash Scripting

The bash input parameter is one of the most integral parts of bash scripting. Here I have listed some scripts that include bash input parameters.

Case 01: Display the Passed Parameter

You can easily display the parameter you passed to the terminal. Here I will pass two parameters while running a script. This script will print the passed parameters to the terminal. to do the same follow the steps below:

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Then, execute the following command into the terminal.

nano parameter.sh

➌ This will open a file named parameter.sh. Now, type the following line into the file, press CTRL+S to save, then CTRL+X to close the file.

#!/bin/bash

echo "First Parameter: $1" #Printing the first parameter
echo "Second Parameter: $2" #Printing the second parameter
EXPLANATION
The Bash script starts with the #!/bin/bash line which specifies that it is a bash script. Then echo “First Parameter: $1” prints ‘First Parameter: $1’ where $1 fetches the value of the first parameter and puts that in the place of ‘$1’. Then echo “Second Parameter: $2” prints ‘Second Parameter: $2’ where $2 fetches the value of the second parameter and puts that in the place of ‘$1’.

➍ Now, run the following command into the terminal to run the parameter.sh bash script passing 10 and 95 as parameters.

bash parameter.sh 10 95

The bash script has printed the two parameters passed into the script.The above image shows that two parameters 10 and 95 are successfully passed to the bash script.

Case 02: Assign Provided Arguments to Bash Variable

While executing the Bash script, it creates the necessary bash variables. Here I will pass two parameters to the bash script. And the bash script will eventually create a bash variable and print it to the terminal. You can follow the scripts to do the same.

You can follow the steps of Case 01 to know about creating and saving shell scripts.

Script (bashvar.sh) >

#!/bin/bash

a=$1
b=$2

p=$(($a*$b))

echo "Product of $a and $b is: $p"

EXPLANATION
a=$1 assigns the value of the first parameter to ‘a’ and b=$2 assigns the value of the second parameter to ‘b’. Then p=$(($a*$b)) does a mathematical multiplication of a and b and keeps the result on the new bash variable p. Afterwards, echo “Product of $a and $b is: $p” prints ‘Product of $a and $b is: $p’ where $a, $b and $p fetch the value of a, b and p and put them on the place of $a, $b and $p respectively.

Now, execute the following command into the terminal to pass the two-parameter 2 and 5 to the bashvar.sh bash script.

bash bashvar.sh 2 5

The bash script has created a bash variable and print it on the terminal.The above image shows that your two passed parameters have created a bash variable which has been printed on the terminal.

Case 03: Check Whether the Passed Parameter is NULL

You can check whether your passed parameter is NULL or not. Here I will develop a bash script and then pass a parameter to this script to check whether the parameter is NULL or not. To achieve so you can follow the given script.

You can follow the steps of Case 01 to know about creating and saving shell scripts.

Script (nullchk.sh) >

#!/bin/bash

if [[ -z $1 ]];
then
echo 'No Parameter Passed!'

else
echo "The Parameter is: $1"

fi

EXPLANATION
if [[ -z $1 ]]; initiates the if else condition and the condition is specified inside the double braces ’[[ ]]’. Here the condition is to check the equality of the variable with empty or null. If so then the echo ‘No Parameter Passed!’ command will be executed which will print ‘’No Parameter Passed!’ on the terminal. If the condition is false, which means the variable is not empty or null then the echo “The Parameter is: $1” command will be executed which will print ‘The Parameter is: <variable>’. Here, <variable> is the parameter you passed. fi marks the end of the if else block.

Now, execute the following command into the terminal to run the bash script and pass a parameter.

bash nullchk.sh
bash nullchk.sh 5

The bash script has checked whether the passed parameter is null or not.The former image illustrates that the bash scripts can check whether a parameter is NULL or not.

Case 04: Reading Multiple Arguments With “for” Loop

Here I will develop a bash script that can take multiple parameters from the terminal and then print them on the terminal. To do the same, follow the below script.

You can follow the steps of Case 01 to know about creating and saving shell scripts.

Script (multiarg.sh) >

#!/bin/bash

for i in $@
do
echo -e "$i\n"

done

EXPLANATION
for i in $@  initiates a for loop and takes the first parameter from all the parameters which are passed to the scripts and sets as i then execute the echo -e “$i\n” command which prints the variable on the terminal. Here -e enables the interpretation of backslash escapes, allowing the newline character to be recognized. After executing this, the next parameter is set as i and the same task is executed till the last parameter. The done marks the end of the for loop.

Now, execute the following command into the terminal to multiple parameters to bash the script.

bash multiplearg.sh 1 2 33 45 1 “hi”

The bash script has printed all the parameters passed to the script.The above image illustrates that the bash script takes multiple parameters and prints them on the terminal.

Case 05: Accessing the Number of Parameters Passed

Here I will develop a bash script that can count the number of parameters passed. To achieve so, you can follow the below script

You can follow the steps of Case 01 to know about creating and saving shell scripts.

Script (parameternum.sh) >

#!/bin/bash

echo "The Number of Parameter Passed In are: $#"
EXPLANATION
The echo “The Number of Parameter Passed In are: $#” command prints the ‘The Number of Parameter Passed In are: $#’ where $# is replaced by the total number of parameters passed to the script.

Now, execute the following command into the terminal to multiple parameters to bash the script.

bash parameternum.sh 1 5 3 5

The bash script has counted the number of parameters passed to the script.The above image illustrates that the bash script counts the total number of parameters passed.

Conclusion

In conclusion, mastering Bash input parameters is essential for enhancing your scripting capabilities. By utilizing them effectively, you can create dynamic and versatile scripts that handle various scenarios. Through practical examples, I’ve demonstrated the power and flexibility of input parameters in Bash. Incorporating these parameters into your scripts will streamline your process, improve efficiency, and empower you as a Bash programmer. Unlock the full potential of Bash by leveraging input parameters. Happy scripting!

People Also Ask

How do I pass input to a bash script?
You have to just type the input you want to pass to your bash script after the name of your bash script.
What are the different types of parameters in Bash?
Generally, Bash has three parameter types. These are variables, positional, and special parameters.
How do I set variables in bash?
The easiest way to set environment variables in Bash is to use the “export” keyword followed by the variable name, an equal sign and the value to be assigned to the environment variable.
Are there data types in bash?
No data types are specific here. In Bash, a variable might contain a number, a character, or a string of characters. The user does not need to define a variable priorly, he/she just needs to assign a value to its reference and it will be created automatically.

Related Articles


<< Go Back to Bash Input | Bash I/O | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment