Ravi's comment is essentially the answer. Functions take their own arguments. If you want them to be the same as command line arguments, you must pass them. Otherwise, you explicitly call the function with no arguments.
However, if you like to store command line arguments in a global array for use in other functions:
my_function() { echo "stored arguments:" for arg in "${commandline_args[@]}"; do echo " $arg" done } commandline_args=("$@") my_function
You need to access the command line arguments using the commandline_args variable, not $@ , $1 , $2 , etc., but they are available. I do not know how to directly assign an array of arguments, but if someone knows it, please enlighten me!
Also notice how I used and quoted $@ - this is how you guarantee that special characters (spaces) will not be removed.
Cascabel Apr 29 2018-10-22T00: 00Z
source share