How to access command line arguments of the caller inside a function?

后端 未结 8 710
囚心锁ツ
囚心锁ツ 2020-11-28 07:31

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

相关标签:
8条回答
  • 2020-11-28 08:10
    # 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
    
    0 讨论(0)
  • 2020-11-28 08:14
    #!/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: please see my comment on question

    0 讨论(0)
  • 2020-11-28 08:14

    Ravi's comment is essentially the answer. Functions take their own arguments. If you want them to be the same as the command-line arguments, you must pass them in. Otherwise, you're clearly calling a function without arguments.

    That said, you could if you like store the command-line arguments in a global array to use within other functions:

    my_function() {
        echo "stored arguments:"
        for arg in "${commandline_args[@]}"; do
            echo "    $arg"
        done
    }
    
    commandline_args=("$@")
    
    my_function
    

    You have to access the command-line arguments through the commandline_args variable, not $@, $1, $2, etc., but they're available. I'm unaware of any way to assign directly to the argument array, but if someone knows one, please enlighten me!

    Also, note the way I've used and quoted $@ - this is how you ensure special characters (whitespace) don't get mucked up.

    0 讨论(0)
  • 2020-11-28 08:14

    One can do it like this as well

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

    Now call your script like

    ./function_test.sh argument1 argument2
    
    0 讨论(0)
  • 2020-11-28 08:15

    My solution:

    Create a function script that is called earlier than all other functions without passing any arguments to it, like this:

    ! /bin/bash

    function init(){ ORIGOPT= "- $@ -" }

    Afer that, you can call init and use the ORIGOPT var as needed,as a plus, I always assign a new var and copy the contents of ORIGOPT in my new functions, that way you can keep yourself assured nobody is going to touch it or change it.

    I added spaces and dashes to make it easier to parse it with 'sed -E' also bash will not pass it as reference and make ORIGOPT grow as functions are called with more arguments.

    0 讨论(0)
  • 2020-11-28 08:24

    My reading of the bash ref manual says this stuff is captured in BASH_ARGV, although it talks about "the stack" a lot.

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

    Save in 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 
    
    0 讨论(0)
提交回复
热议问题