SoFunction
Updated on 2025-04-08

The shell handles the user input and pass parameters.

The most basic way to pass data to a shell script is to use command line parameters. Command line parameters allow data to be added to the command line when running scripts:

$ ./addem 10 30

This example passes two command line parameters (10 and 30) to the script addem. The script handles command line parameters through special variables. Next, we will introduce how to use command line parameters in bash shell scripts.

Read parameters

The bash shell assigns all command line parameters to a special variable called positional parameters. This also includes shell script names. The names of position variables are all standard numbers: $0 corresponds to the script name, $1 corresponds to the first command line parameter, $2 corresponds to the second command line parameter, and so on, until $9.

Here is a simple example of using a single command line argument in a shell script:

$ cat 
#!/bin/bash
# Using one command-line parameter
#
factorial=1
for (( number = 1; number <= $1; number++ ))
do
    factorial=$[ $factorial * $number ]
done
echo The factorial of $1 is $factorial
exit
$
$ ./ 5
The factorial of 5 is 120

In shell scripts, you can use the $1 variable just like you would with other variables. The shell script will automatically assign the value of the command line parameter to the position variable without any special processing.

If more command line parameters need to be entered, the parameters must be separated by spaces. The shell will assign it to the corresponding position variable:

$ cat 
#!/bin/bash
# Using two command-line parameters
#
product=$[ $1 * $2 ]
echo The first parameter is $1.
echo The second parameter is $2.
echo The product value is $product.
exit
$
$ ./ 2 5
The first parameter is 2.
The second parameter is 5.
The product value is 10.

In the above example, the command line parameters used are all numeric values. You can also use text strings as parameters on the command line:

$ cat 
#!/bin/bash
# Using one command-line string parameter
#
echo Hello $1, glad to meet you.
exit
$
$ ./ world
Hello world, glad to meet you.

The shell passes the string value as a command line parameter to the script. But if you encounter a string containing spaces, the problem will occur:

$ ./ big world
Hello big, glad to meet you.

Remember, parameters are separated by spaces, so the shell will treat the spaces contained in the string as separators of the two parameters. To add spaces to parameter values, you must use quotes (single or double quotes are available).

$ ./ 'big world'
Hello big world, glad to meet you.
$
$ ./ "big world"
Hello big world, glad to meet you.

Note When passing text strings as parameters, quotes are not part of the data and are only used to indicate the start and end positions of the data.

If the script needs more than 9 command line parameters, you can continue to add more parameters, but you need to slightly modify the location variable name. After the 9th position variable, curly braces must be added on both sides of the variable name, such as ${10}. Let’s take a look at an example:

$ cat 
#!/bin/bash
# Handling lots of command-line parameters
#
product=$[ ${10} * ${11} ]
echo The tenth parameter is ${10}.
echo The eleventh parameter is ${11}.
echo The product value is $product.
exit
$
$ ./ 1 2 3 4 5 6 7 8 9 10 11 12
The tenth parameter is 10.
The eleventh parameter is 11.
The product value is 110.

This way you can add as many command line parameters to the script as you want.

Read script name

You can use the position variable $0 to get the shell script name that runs on the command line. This is very convenient when writing tools that include multiple functions or generate log messages.

$ cat 
#!/bin/bash
# Handling the $0 command-line parameter
#
echo This script name is $0.
exit
$
$ bash 
This script name is .

But there is a potential problem here. If you use another command to run the shell script, the command name will be mixed with the script name and appear in the location variable $0:

$ ./
This script name is ./.

This is not the only problem. If the absolute path is used when running the script, the position variable $0 will contain the entire path:

$ $HOME/scripts/
This script name is /home/christine/scripts/.

If you write a script that only intends to use the script name, you have to do some extra work and strip the script's running path. Fortunately, there is a convenient little command that can help us. The basename command can return the script name that does not contain the path:

$ cat 
# Using basename with the $0 command-line parameter
#
name=$(basename $0)
#
echo This script name is $name.
exit
$
$ ./
This script name is .

It's much better now. You can use this technique to write a script that generates log messages that identifies the runtime:

$ cat 
#!/bin/bash
# Using the $0 command-line parameter in messages
#
scriptname=$(basename $0)
#
echo The $scriptname ran at $(date) >> $HOME/
exit
$
$ ./
$ cat $HOME/
The  ran at Wed 17 Jul 2024 19:00:53 AM EDT

Having a script that recognizes itself is very useful for tracking script issues, system audits, and generating log information.

Parameter testing

Be careful when using command line parameters in shell scripts. If the required parameters are not specified when running the script, problems may occur:

$ ./
./: line 5: (( number <= : syntax error:
operand expected (error token is "<= ")
The factorial of is 1

When the script thinks that there should be data in the location variable, but in fact there is no such thing as it is, the script is likely to generate an error message. This method of writing scripts is not advisable. Be sure to check if it is empty before using position variables:

$ cat 
#!/bin/bash
# Using one command-line parameter
#
if [ -n "$1" ]
then
    factorial=1
    for (( number = 1; number <= $1; number++ ))
    do
        factorial=$[ $factorial * $number ]
    done
    echo The factorial of $1 is $factorial
else
    echo "You did not provide a parameter."
fi
exit
$
$ ./
You did not provide a parameter.
$
$ ./ 3
The factorial of 3 is 6

The above example uses the -n test to check whether the command line parameter $1 is empty.

This is the article about the implementation of shell processing user input and passing parameters. For more relevant shell users input and passing parameters, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!