How do I prevent bash to use a builtin command?

后端 未结 2 1482
清歌不尽
清歌不尽 2020-12-17 17:55

I\'m trying to fix a script that use echo, that is using the builtin command instead of the command, how can I prevent that?

I know I can do /bin/

2条回答
  •  清歌不尽
    2020-12-17 18:26

    Use the env program. Env is a command which launches another program with a possibly modified environment. Because env is a program, it doesn't have access to shell builtins, aliases, and whatnot.

    This command will run the echo program, searching for it in your command path:

    $ env echo foo
    

    You can verify this by using strace to monitor system calls while running echo vs env echo:

    $ strace -f -e trace=process bash -c 'echo foo'
    execve("/bin/bash", ["bash", "-c", "echo foo"], [/* 16 vars */]) = 0
    arch_prctl(ARCH_SET_FS, 0x7f153fa14700) = 0
    foo
    exit_group(0)                           = ?
    
    $ strace -f -e trace=process bash -c 'env echo foo'
    execve("/bin/bash", ["bash", "-c", "env echo foo"], [/* 16 vars */]) = 0
    arch_prctl(ARCH_SET_FS, 0x7f474eb2e700) = 0
    execve("/usr/bin/env", ["env", "echo", "foo"], [/* 16 vars */]) = 0
    arch_prctl(ARCH_SET_FS, 0x7f60cad15700) = 0
    execve("/usr/local/sbin/echo", ["echo", "foo"], [/* 16 vars */]) = -1 ENOENT (No such file or directory)
    execve("/usr/local/bin/echo", ["echo", "foo"], [/* 16 vars */]) = -1 ENOENT (No such file or directory)
    execve("/usr/sbin/echo", ["echo", "foo"], [/* 16 vars */]) = -1 ENOENT (No such file or directory)
    execve("/usr/bin/echo", ["echo", "foo"], [/* 16 vars */]) = -1 ENOENT (No such file or directory)
    execve("/sbin/echo", ["echo", "foo"], [/* 16 vars */]) = -1 ENOENT (No such file or directory)
    execve("/bin/echo", ["echo", "foo"], [/* 16 vars */]) = 0
    arch_prctl(ARCH_SET_FS, 0x7f0146906700) = 0
    foo
    exit_group(0)                           = ?
    

提交回复
热议问题