I\'m attempting to write a function in bash that will access the scripts command line arguments, but they are replaced with the positional arguments to the function. Is ther
You can use the shift keyword (operator?) to iterate through them. Example:
#!/bin/bash
function print()
{
while [ $# -gt 0 ]
do
echo $1;
shift 1;
done
}
print $*;
If you want to have your arguments C style (array of arguments + number of arguments) you can use $@
and $#
.
$#
gives you the number of arguments.
$@
gives you all arguments. You can turn this into an array by args=("$@")
.
So for example:
args=("$@")
echo $# arguments passed
echo ${args[0]} ${args[1]} ${args[2]}
Note that here ${args[0]}
actually is the 1st argument and not the name of your script.