Command-line parameters are an efficient way to control the behavior of your shell scripts. They can make your scripts more dynamic, easier to use, and more powerful. In this blog post, we will delve into five ways you can pass command-line parameters to a Linux shell script.
First, a little about various Linux Shells…
Linux shells, including the Bourne Shell (sh), Bourne Again Shell (bash), C Shell (csh), Korn Shell (ksh), and Z Shell (zsh), are command-line interpreters that allow users to interact with the operating system. Bash, a superset of sh, is the most common, offering scripting features like variables and control structures. Csh, with a syntax similar to C programming, is appreciated for its interactive features. Ksh combines features from both sh and csh. Lastly, zsh, a robust shell, provides superior scripting, auto-completion, and customization options, making it a favorite among power users.
Table of Contents
1. Positional Parameters (Arguments)
Positional parameters are one of the most straightforward ways to pass command-line arguments to a script.
Here is how you can do it:
- Open a text editor and create a new shell script. Let’s name it
script.sh
. - Write the following code:
#!/bin/bash
echo "The first parameter is $1."
echo "The second parameter is $2."
- Save and close the file.
- Now, make the script executable using the following command:
chmod +x script.sh
- Run the script with two parameters:
./script.sh Hello World
You will see the output:
The first parameter is Hello.
The second parameter is World.
2. Special Variables
Apart from the positional parameters, there are special variables that you can use to get more information about the arguments.
Here is how you can use them:
- Modify the
script.sh
file and add the following code:
#!/bin/bash
echo "The script name is $0."
echo "The total number of parameters is $#."
echo "All parameters are $@."
- Save and close the file.
- Run the script with three parameters:
./script.sh param1 param2 param3
You will see the output:
The script name is ./script.sh.
The total number of parameters is 3.
All parameters are param1 param2 param3.
3. Shift Command
The shift
command is used to shift positional parameters to the left.
Here’s how you can use it:
- Modify the
script.sh
file and add the following code:
#!/bin/bash
while (( "$#" )); do
echo "Parameter: $1"
shift
done
- Save and close the file.
- Run the script with three parameters:
./script.sh A B C
You will see the output:
Parameter: A
Parameter: B
Parameter: C
4. getopts Function
The getopts
function is a powerful way to parse command-line options and parameters.
Here is an example:
- Modify the
script.sh
file and add the following code:
#!/bin/bash
while getopts ":a:b:" opt; do
case ${opt} in
a ) echo "Option a has value $OPTARG"
;;
b ) echo "Option b has value $OPTARG"
;;
\? ) echo "Invalid Option: -$OPTARG" 1>&2
;;
esac
done
- Save and close the file.
- Run the script with two options:
./script.sh -a value1 -b value2
You will see the output:
Option a has value value1
Option b has value value2
5. Reading from Standard Input
Finally, you can pass parameters to a script by reading from standard input.
Here’s how:
- Modify the
script.sh
file and add the following code:
!/bin/bash
echo "Please enter something:"
read input
echo "You entered: $input"
``