About
An argument is a parameter given:
- to a command
- to a function
- or to the bash shell
They are referenced by position.
A positional parameter is a parameter denoted:
- by one or more digits,
- other than the single digit 0.
Positional parameters are assigned from the shell’s arguments when it is invoked, and may be reassigned using the set builtin command. Positional parameters may not be assigned to with assignment statements. The positional parameters are temporarily replaced when a shell function is executed. When a positional parameter consisting of more than a single digit is expanded, it must be enclosed in braces.
How to
How to loop or parse them?
To parse positional parameters, you can use the getopts command
but you can also loop over them:
- with for:
for arg in "$@"; do
echo "$arg"
done
- With while
while [[ $# -gt 0 ]]
do
echo $1
shift
done
How to extract a command as first argument
# Assign the first argument to a string
COMMAND=${1:-}
if [ "$COMMAND" = "" ]; then
echo "A command is mandatory"
exit 1
fi
shift # Remove the first argument from the argument list
# "$@" is then the rest of the arguments
case $COMMAND in
"hello")
hello_function "$@"
;;
*)
echo "Command $COMMAND is unknown"
exit 1
esac
How to normalize options ?
getopt is a helper that:
- handle different options format such as --arg=option or --arg option
- and normalize them
args=$(getopt -l "no-header:,help" -o "s:h" -- "$@")
# eval set to set the positional arguments back to $args
eval set -- "$args"
where:
- l specifies a set of long option value separated by a colon. ie
- no-header: means that we expect a –no-header option with a value
- help means that we expect a –help option without a value
- o specifies a set of short option value separated by a colon
- s: means that we expect a -s option with a value
- h means that we expect a -h option without a value
How to parse arguments from stdin?
You can generate arguments with Xargs
Example:
echo argument1 argument2 argument3 | xargs bash -c 'echo This is third arg : $2'
Special Parameters
There is some special parameter that gives extra function around positional parameters.
$0
The script name.
echo $0
/bin/bash
Star ($*) - Single Word
* Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable.
That is, $* is equivalent to $1c$2c..., where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.
$*: All of the positional parameters, seen as a single word
“$*” must be quoted.
$1, $2, etc.
Positional parameters, passed:
At ($@)
@ expands to the positional parameters, starting from one.
- When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1", "$2" …
- If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word.
- When there are no positional parameters,"$@" and $@ expand to nothing (i.e., they are removed).
Number of positional parameters (# Hashtag)
$#:
- Number of command-line arguments or positional parameters
- expands to the number of positional parameters in decimal.
echo $#
0
Explicit Null
The explicit null parameters are:
""
# or
''
Management
Shift method
Test if set
Because an argument is a parameter, we can use the parameter expansion in a conditional expression to test if it's set.
- with the z conditional operator true if the variable/argument is not set
if [ -z ${1+x} ]; then echo "1 is unset"; else echo "1 is set to '$1'"; fi
# or
if !([ -z ${1+x} ]); then echo "1 is set to '$1'"; else echo "1 is unset "; fi
- with the n conditional operator true if the variable/argument is set
if [ -n ${1+x} ]; then echo "1 is set to '$1'"; else echo "1 is unset"; fi
# or
if !([ -n ${1+x} ]); then echo "1 is no set"; else echo "1 is set to '$1'"; fi
Passing
- When passing a string as an argument to a function, if the string is not quoted it will be parsed according to the IFS separator
Bash - How to pass arguments that have space in their values ?
Parsing
If the script expect the below arguments:
./script.sh --server serviceName --silent
It can parse them like that:
while [[ $# -gt 0 ]]
do
case "$1" in
--service)
# Run in service mode
shift # past argument
SERVICE_FILE=$1
shift
;;
--silent)
SILENT_MODE=1
shift
;;
*)
# unknown option
shift # past argument with no value
;;
esac
done
with the help of:
- and Bash - Shift
How to pass a generated file with Process substitution
If a script expect only a path such as openssl storeutl, you can fake it with Process_substitution
Example: If the cli script expect only a path, you can still give it generated content like that:
script <(echo "Content")