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

后端 未结 8 916
囚心锁ツ
囚心锁ツ 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:25

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

    #!/bin/bash
    function print()
    {
        while [ $# -gt 0 ]
        do
            echo $1;
            shift 1;
        done
    }
    print $*;
    

提交回复
热议问题