Passing arguments by reference

后端 未结 9 1944
挽巷
挽巷 2020-12-01 01:13

I want to ask if it is possible to pass arguments to a script function by reference:

i.e. to do something that would look like this in C++:



        
9条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-01 01:50

    I have found a way to do this but I am not sure how correct this is:

    Newfun()
    {
        local var1="$1"
        eval $var1=2
        # or can do eval $1=2 if no local var
    }
    
    var=1
    echo  var is $var    # $var = 1
    newfun 'var'         # pass the name of the variable…
    echo now var is $var # $var = 2
    

    So we pass the variable name as opposed to the value and then use eval ...

提交回复
热议问题