Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access arguments to Bash script inside a function [duplicate]

Tags:

bash

Inside a function, $1 ... $n are the parameters passed to that function. Outside a function $1 ... $n are the parameters passed to the script.

Can I somehow access the parameters passed to the script inside a function?

like image 339
zedoo Avatar asked Oct 19 '10 07:10

zedoo


People also ask

How do you access these arguments from within the script?

Using arguments Inside the script, we can use the $ symbol followed by the integer to access the arguments passed. For example, $1 , $2 , and so on. The $0 will contain the script name.

How do you access command line arguments in bash?

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=("$@") .

How do you pass a command line argument to a function in shell script?

We can use the getopts program/ command to parse the arguments passed to the script in the command line/ terminal by using loops and switch-case statements. Using getopts, we can assign the positional arguments/ parameters from the command line to the bash variables directly.


2 Answers

Usually you just pass them as parameters to the function at call time.

The (uglier) alternative is to put them in global variables.

like image 110
Jean Avatar answered Sep 21 '22 13:09

Jean


(I know this is an old post, but none of the answers actually answered the question.)

Use the BASH_ARGV array. It contains the arguments passed to the invoking script in reverse order (i.e., it's a stack with the top at index 0). You may have to turn on extended debugging in the shebang (e.g., #!/bin/bash -O extdebug) or with shopt (e.g., shopt -s extdebug), but it works for me in bash 4.2_p37 without it turned on.

From man bash:

An array variable containing all of the parameters in the current bash execution call stack. The final parameter of the last subroutine call is at the top of the stack; the first parameter of the initial call is at the bottom. When a subroutine is executed, the parameters supplied are pushed onto BASH_ARGV. The shell sets BASH_ARGV only when in extended debugging mode….

Here's a function I use to print all arguments in order on a single line:

# Print the arguments of the calling script, in order. function get_script_args {     # Get the number of arguments passed to this script.     # (The BASH_ARGV array does not include $0.)     local n=${#BASH_ARGV[@]}      if (( $n > 0 ))     then         # Get the last index of the args in BASH_ARGV.         local n_index=$(( $n - 1 ))          # Loop through the indexes from largest to smallest.         for i in $(seq ${n_index} -1 0)         do             # Print a space if necessary.             if (( $i < $n_index ))             then                 echo -n ' '             fi              # Print the actual argument.             echo -n "${BASH_ARGV[$i]}"         done          # Print a newline.         echo     fi } 
like image 21
Dean Hall Avatar answered Sep 22 '22 13:09

Dean Hall