Bash How to Pass Arguments
The special shell variable “$@” represents a list of all arguments that is passed to the script.
If you want to pass all arguments to your function, you can use this syntax:
function_name "$@"
If you want to pass all arguments to another script, you can use this syntax:
script_mame "$@"
Let’s take an example called passit.sh. In this script, we defined function print_argument, that print argument that comes from the command line:
#!/bin/bash
# function's definition
function PRINT_ARGUMENTS()
{
echo "Arguments of shell are: $@"
}
# in this place we want to call function
PRINT_ARGUMENTS "$@"
Let’s try to execute passit.sh in this way:
chmod u+x ./passit.sh ./passit.sh aa bb cc
Output: Arguments of the shell are: aa bb cc
You can see that function obtains all shell arguments, which were written in the bash command line.
To get a number of arguments use:
echo "Number of arguments of shell are: $#"
It is often used to check if a required number is equal to some value.