How can you use pure unset shell builtin? Can you write shell scripts that are immune to tampering?

前端 未结 3 1673
时光取名叫无心
时光取名叫无心 2021-01-01 07:32

I mean I want to use unset that is not a shell function itself. If I could do that, I could make sure that command is pure by running



        
3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-01 08:21

    funnily enough, you already said the builtin name -- command

    $ var="FOO"
    $ unset() { echo nope; }
    $ echo "${var}"
    FOO
    $ unset var
    nope
    $ echo "${var}"
    FOO
    $ command unset var
    $ echo "${var}"
    
    

    this doesn't help if you're in a hostile environment where someone has created a command() { :; } function. but if you're in a hostile environment, you've already lost ;).

    when it comes to exporting functions into the environment, that's a bash-specific extension and you should not really rely on that. POSIX shells (like dash) do not support that by design.

提交回复
热议问题