How to change argv[0] value in shell / bash script?

前端 未结 3 859
无人及你
无人及你 2021-01-15 05:11

The set command can be used to change values of the positional arguments $1 $2 ...

But, is there any way to change $0 ?

3条回答
  •  鱼传尺愫
    2021-01-15 05:40

    In Bash greater than or equal to 5 you can change $0 like this:

    $ cat bar.sh
    #!/bin/bash
    echo $0
    BASH_ARGV0=lol
    echo $0
    $ ./bar.sh 
    ./bar.sh
    lol
    

    ZSH even supports assigning directly to 0:

    $ cat foo.zsh
    #!/bin/zsh
    echo $0
    0=lol
    echo $0
    $ ./foo.zsh 
    ./foo.zsh
    lol
    

提交回复
热议问题