How to change a command line argument in Bash?

前端 未结 3 1073
野性不改
野性不改 2020-12-02 10:55

Is there a way to change the command line arguments in a Bash script? For example, a Bash script is invoked like this:

./foo arg1 arg2  

Is t

3条回答
  •  猫巷女王i
    2020-12-02 11:39

    I know this is an old one but I found the answer by thkala very helpful, so I have used the idea and expanded on it slightly to enable me to add defaults for any argument which has not been defined - for example:

    
        # set defaults for the passed arguments (if any) if not defined.
        #
        arg1=${1:-"default-for-arg-1"}
        arg2=${2:-"default-for-arg-2"}
        set -- "${arg1}" "${arg2}"
        unset arg1 arg2
    
    

    I hope this is of use to someone else.

提交回复
热议问题