Skip to content

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

As an Amazon Associate, I earn from qualifying purchases.


LPI Linux Essentials Exam 010-160 - Topic 3.3 - Processing Shell Arguments / Inputs

When crafting shell scripts, processing user inputs is a fundamental operation. The shell environment offers built-in variables to facilitate the management of these inputs, which can be accessed in the following ways:

Positional parameters, denoted as $1, $2, $3, and so forth, correspond to the arguments provided during the script's execution. For instance, if a script is run with ./script.sh arg1 arg2, the shell assigns the string arg1 to $1 and arg2 to $2. If additional arguments are passed, they continue to be assigned to subsequent positional variables accordingly.

The variable $@ is a special parameter that holds all positional parameters, treating each as a separate quoted string. This characteristic is particularly beneficial when iterating over arguments is necessary, as it preserves the integrity of inputs containing spaces or other delimiters that could alter their interpretation.

Conversely, $* consolidates all positional parameters into a single string, which is valuable when the collective input should be handled as a unified block.

Another built-in variable, $#, indicates the total count of positional parameters passed to the script.

It is advisable to encapsulate these variables in double quotes, such as "$1" or "$@", especially when dealing with inputs that may include spaces, special characters, or wildcard expressions. This practice prevents the shell from misinterpreting such characters and ensures the arguments are processed as intended.

Crafting a Script to Process Inputs

The exercises below will illustrate the practical application of input variables:

Exercise: Create a script that processes input variables:

Ensure a suitable location is used to store the script

mkdir -p ~/scripts
cd ~/scripts

Using a here-document, generate the process_inputs.sh script:

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"
echo ""
echo "Iterating over each argument:"
for arg in "$@"
do
    echo "    - $arg is an argument"
done
echo ""
echo "Displaying all arguments as one entity:"
echo "    - $*"
echo ""
echo "The total number of arguments is: $#"
echo ""
EOF

Adjust the script's permissions to make it executable:

chmod u+x process_inputs.sh

This initial segment constructs the environment and script required for testing input variables.

Running process_inputs.sh with Various Arguments

To examine the script's behavior, follow the exercises to execute it with different arguments:

Exercise: Supply three arguments to the script:

./process_inputs.sh tiger shark bear

This should produce output lines showing each argument and the total count:

The first argument is: tiger
The second argument is: shark
The third argument is: bear

Iterating over each argument:

    - tiger is an argument
    - shark is an argument
    - bear is an argument

Displaying all arguments as one entity:

    - tiger shark bear

The total number of arguments is: 3

Exercise: Supply four arguments to the script:

./process_inputs.sh tiger shark bear eagle

This exercise demonstrates the script's capability to handle multiple arguments using $@, $*, and $#, showcasing the flexibility of shell scripts in varying input conditions.

The output will resemble:

The first argument is: tiger
The second argument is: shark
The third argument is: bear

Iterating over each argument:

    - tiger is an argument
    - shark is an argument
    - bear is an argument
    - eagle is an argument

Displaying all arguments as one entity:

    - tiger shark bear eagle

The total number of arguments is: 4

Exercise: Supply three arguments to the script, utilizing quotes to treat to arguments as one entity:

./process_inputs.sh 'tiger shark' bear eagle

Quotation marks are employed to treat tiger shark as a singular argument, demonstrating how to handle inputs that encompass spaces.

The output will resemble:

The first argument is: tiger shark
The second argument is: bear
The third argument is: eagle

Iterating over each argument:

    - tiger shark is an argument
    - bear is an argument
    - eagle is an argument

Displaying all arguments as one entity:

    - tiger shark bear eagle

The total number of arguments is: 3

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.