*nix command to force use of a native built-in coreutils command.


"'builtin cp' or the like may just get you out of trouble with others' copied scripts."
—WIsdom from my linux users group.


We've all done it; created an alias for something like ls or cp so we can tweak the bare command to be something more useful to us. or we may have created a script or function that uses the name of a built-in command. then all of a sudden we find that we need access to the native bultin command. That's where the command builtin comes to the rescue. Using builtin {command} forces your shell to bypass any custom aliases, functions or scripts of the same name and calls the command directly from the shell. Given that i've aliased cd to zoxide for example,, I occasionally need to use builtin cd, for example.

It's worth adding that the backslash before a command can have the same effect, so in my case, "\ls" skips the alias I have set and runs the bare ls command, so my 'alias ls='ls -aFh --color=always' is ignored, passed over and the raw shell command used instead.

Why might I need to know these tricks? Well let's just say that I have a script that uses a shell built-in. If that script tries to use my aliased command, if might fail to produce the desired results, hence I must edit the script to add 'builti'n before the required command. I find this most often happens with scripts I've copied or downloaded from somewhere, so if you find yourself in this position, be prepared to edit the script accordingly. it happens more frequently than i would have thought; a truly thoughtful script author may even have added this, knowing that some builtins are likely to have been aliased by others.

All this said, do beware; some utilities are duplicated by the shell and coreutils, not all coreutils are built in to the shell itself. Built-ins are faster because they don't require creating a new process, some commands need to be built-ins because they modify the shell's internal state (like cd). Coreutils versions often offer extra functionality or options that built-ins don’t have.

Some commands (like echo and pwd) remain built-ins for convenience and speed, even though they could be external commands. As far as i can see, these commands tend to be duplicated:

echo, pwd, test, true, false, printf, command, type, hash

If you know of more, let me know, please.






$ xclip -o | wc -w
288

Disclaimer: i wrote this outdoors in blazing sunlight and couldn't read the screen well enough to pick up my typos; please have some understanding and patience with an old man's poor eyesight ♥

C-Dawg says re builtin: Actually, there are fewer typos here than normally! Get out in the sun more :)