Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


RHCSA - Create Simple Shell Scripts: Process Script Inputs ($1, $2, $@, $*, $#)

Input Variables

When writing shell scripts, you often need to process inputs and the shell provides several variables to handle these inputs:

  • $1, $2, $3, ...: These variables represent the positional parameters passed to the script. When you execute a script with arguments, such as ./script.sh arg1 arg2, the value of $1 will be "arg1," and the value of $2 will be "arg2." The $3, $4, and so on, represent subsequent arguments if provided.
  • $@: This variable represents all the positional parameters passed to the script as individual quoted strings. It's commonly used when you want to iterate over each argument.
  • $*: This variable represents all the positional parameters passed to the script as a single string. It's useful when you want to treat all the arguments as a single entity.
  • $#: This variable represents the number of positional parameters passed to the script.

When using these variables, it's generally a good practice to enclose them in double quotes ("$1", "$@", etc.) to handle cases where the argument might contain spaces or special characters.

Creating a Script to Process Inputs

You will create a script that allows you to process the inputs that you pass it as arguments and then test that the input variables work as expected.

Create a script to process inputs:

mkdir -p ~/scripts
cd ~/scripts

cat << 'EOF' > process_inputs.sh 
#!/bin/bash

echo "The first argument is: $1"
echo "The second argument is: $2"
echo "The third argument is: $3"
for args in "$@"
do
    echo "$args is one of the arguments passed to the script"
done
for args in "$*"
do
    echo "$args is all of the arguments passed to the script together as 1"
done
echo "Total number of arguments: $#"

EOF

chmod u+x process_inputs.sh

First we ensure that we are in a suitable location for a script, then we create process_inputs.sh with echo commands to test that the special input variables perform as expected.

Run process_inputs.sh with 3 arguments:

./process_inputs.sh tiger shark bear

The script output shows as expected.

Run process_inputs.sh with 4 arguments:

./process_inputs.sh tiger shark bear eagle

Our script streamlines coding by using $@ and $* to handle a collective array of variables and $# to count them, thereby accommodating any number of arguments seamlessly. This method efficiently simplifies the management of multiple inputs, eliminating the need for individual variable referencing and making the script more adaptable to varying input scenarios.

Run process_inputs.sh with 3 quoted arguments:

./process_inputs.sh 'tiger shark' bear eagle

When using quotes, even if there is spaces between the inputs that are quoted, they will get treated as one argument. Handy to know as we specifically meant tiger shark as one animal.

Be sure to experiment to become comfortable with processing shell inputs and how quoting can affect the outcome.


Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.