Positional Parameters
In Bash, positional parameters are special variables that hold the arguments passed to a script or function. The positional parameters are numbered from 0
to 9
, where $0
holds the name of the script or function, and $1
through $9
hold the first through ninth arguments, respectively.
$@
and $*
both hold all the arguments passed to the script. Note that $@
is an array-like variable that holds all the positional parameters as separate elements, while $*
is another variable that holds all the positional parameters as a single string.
Here’s an example:
#!/bin/bash
#!/bin/bash
echo "Script Name: $0"
echo "First Arg: $1"
echo "Second Arg: $2"
echo "All args (arr): $@"
echo "All args (str): $*"
Now let’s run the script with three arguments:
1 | chmod u+x ./script # Grant execution privilege for user |
The output should be something like this:
Script Name: ./script.sh
First Arg: a
Second Arg: b
All args (arr): a b c
All args (str): a b c