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/echo
to force the usage of that, but I wouldn't like to hardcode the path (for portability).
I thought using something as:
$ECHO=`which echo`
$ECHO -e "text\nhere"
but which echo
returns: "echo: shell built-in command".
I've ended up defining an echo
function that uses env
as @Kenster recommends. This way I don't need to modify the calls to echo in the script.
echo() {
env echo $*
}
# the function is called before the built-in command.
echo -en "text\nhere"
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) = ?