How to access caller command line arguments inside a function?

I am trying to write a function in bash that will access script command line arguments, but they are replaced with positional arguments to the function. Is there a way for a function to access command line arguments if they are not passed explicitly?

# Demo function function stuff { echo $0 $* } # Echo the name of the script, but no command line arguments stuff # Echo everything I want, but trying to avoid stuff $* 
+80
bash command-line-arguments argv
Apr 29 2018-10-29T00:
source share
7 answers

My reading of the bash manual indicates that this stuff is written to BASH_ARGV, although it talks a lot about the "stack".

 #!/bin/bash function argv { for a in ${BASH_ARGV[*]} ; do echo -n "$a " done echo } function f { echo f $1 $2 $3 echo -nf ; argv } function g { echo g $1 $2 $3 echo -ng; argv f } f boo bar baz g goo gar gaz 

Save to f.sh

 $ ./f.sh arg0 arg1 arg2 f boo bar baz farg2 arg1 arg0 g goo gar gaz garg2 arg1 arg0 f farg2 arg1 arg0 
+41
Apr 29 2018-10-22T00:
source share
— -

If you want to have your own C argument style (array of arguments + number of arguments), you can use $@ and $# .

$# gives you the number of arguments.
$@ gives you all the arguments. You can turn this into an array with args=("$@") .

For example:

 args=("$@") echo $# arguments passed echo ${args[0]} ${args[1]} ${args[2]} 

Note that here ${args[0]} is actually the first argument, not the name of your script.

+85
Apr 29 '10 at 21:40
source share

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.

+15
Apr 29 2018-10-22T00:
source share
 #!/usr/bin/env bash echo name of script is $0 echo first argument is $1 echo second argument is $2 echo seventeenth argument is $17 echo number of arguments is $# 

Edit: see my comment on the question

+11
Apr 29 '10 at 21:33
source share
 # Save the script arguments SCRIPT_NAME=$0 ARG_1=$1 ARGS_ALL=$* function stuff { # use script args via the variables you saved # or the function args via $ echo $0 $* } # Call the function with arguments stuff 1 2 3 4 
+6
Apr 29 '10 at 21:40
source share

You can do it the same way.

 #!/bin/bash # script_name function_test.sh function argument(){ for i in $@;do echo $i done; } argument $@ 

Now call the script like

 ./function_test.sh argument1 argument2 
+2
Jul 14 '16 at 5:06
source share

You can use the shift (operator?) Keyword to iterate through them. Example:

 #!/bin/bash function print() { while [ $# -gt 0 ] do echo $1; shift 1; done } print $*; 
+1
Nov 19 '14 at 21:26
source share



All Articles