4

Consider a situation that I run these commands in my current shell or I put them inside .bashrc:

alias source='echo hi'
alias .='echo hi'
alias unalias='echo hi'

Or function source(){ echo hi; }, etc.

In case of binary commands we can use absolute path like: /bin/ls, however how can I specifically run any of these shell built-in commands inside my current shell?

2 Answers 2

10

Bash has the command builtin for that:

builtin: builtin [shell-builtin [arg ...]]
Execute shell builtins.

Execute SHELL-BUILTIN with arguments ARGs without performing command
lookup.  

E.g.

$ cat > hello.sh
echo hello
$ source() { echo x ; }
$ source hello.sh
x
$ builtin source hello.sh
hello

Nothing prevents you from overriding builtin, however.

Another way to work around aliases (but not functions) is to quote (part of) the word:

$ alias source="echo x"
$ source hello.sh 
x hello.sh
$ \source hello.sh
hello
2
  • Any workaround for functions? function .(){ echo hi; } ; function builtin(){ echo hi; } If I run \builtin It gives me hi.
    – Ravexina
    Commented Apr 30, 2017 at 8:59
  • 1
    @Ravexina, I think the choices are either unset -f builtin or to not override builtin in the first place.
    – ilkkachu
    Commented Apr 30, 2017 at 9:07
6

Aliases can always be bypassed by quoting any part of the command name, e.g. \source or 'source' or ''source or … (unless you've defined aliases for those as well which zsh, but not other shells, allows).

Functions can be bypassed with the command prefix (e.g. command source) in any POSIX shell. In bash or zsh you can use builtin instead of command to force the use of a builtin (command falls back to a PATH lookup if there's no builtin by that name, and in zsh (except when emulating other shells), command skips builtins altogether). You can unset a function with e.g. unset -f source.

If you've overridden or disabled all of builtin, command and unset, you may need to abandon the idea of restoring this shell instance to a reasonable state.

2
  • +1 Thanks, is there any way to disallow overwriting a function like command ?
    – Ravexina
    Commented May 1, 2017 at 8:18
  • @Ravexina Not in any of the usual shells, no. Commented May 1, 2017 at 10:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .