How do I prevent bash to use a builtin command? How do I prevent bash to use a builtin command? bash bash

How do I prevent bash to use a builtin command?


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 */]) = 0arch_prctl(ARCH_SET_FS, 0x7f153fa14700) = 0fooexit_group(0)                           = ?$ strace -f -e trace=process bash -c 'env echo foo'execve("/bin/bash", ["bash", "-c", "env echo foo"], [/* 16 vars */]) = 0arch_prctl(ARCH_SET_FS, 0x7f474eb2e700) = 0execve("/usr/bin/env", ["env", "echo", "foo"], [/* 16 vars */]) = 0arch_prctl(ARCH_SET_FS, 0x7f60cad15700) = 0execve("/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 */]) = 0arch_prctl(ARCH_SET_FS, 0x7f0146906700) = 0fooexit_group(0)                           = ?


You can disable the builtin echo:

enable -n echo

Now simply doing echo anything will run the external version. It only affects the current script process, so you can safely do it in your scripts.