I know what a builtin command is, but what does builtin itself do? Executing which ls shows me /bin/ls, but executing which builtin returns nothing.

man builtin just gives me a list of builtin commands, one of which is builtin. The rest of the man page explains what a builtin is, but not what builtin is.

builtin --help tells me builtin: usage: builtin [shell-builtin [arg ...]] but still not what it does.

Is it part of bash in a way that other builtin commands aren't?

  • 3
    You forgot help builtin. – Ignacio Vazquez-Abrams Aug 17 '17 at 7:28
  • 3
    which builtin returns no results, because which reports the location of external commands. If you ask the shell type builtin, it will tell you, unsurprisingly, that builtin is a shell builtin. You'll find the same with alias, set, etc. – Toby Speight Aug 17 '17 at 10:57
  • 1
    builtin help builtin – theonlygusti Aug 17 '17 at 14:07

The builtin command makes sure you run the shell built-in version of the command rather than running another command with the same name.

For example, let's say you defined a shell function named cd to print some extra status everytime you change directories. But you messed it up and now you can't change directories correctly. So now you can type builtin cd ~ to successfully cd back to your home directory without running your broken shell function.

And by the way, my copy of the bash man page has a section called "SHELL BUILTIN COMMANDS", and it defines the meaning of the builtin command in that section (transcribed below).

builtin shell-builtin [arguments]

    Execute  the  specified  shell builtin, passing it arguments, and
    return its exit status.  This is useful when defining a function
    whose name  is the  same as a shell builtin, retaining the
    functionality of the builtin within the function.  The cd builtin is
    commonly  redefined  this  way.

    The  return status is false if shell-builtin is not a shell builtin
| improve this answer | |
  • 14
    nice answer. But makes me wonder, how can you be sure that the builtin command is the real builtin from bash ? – Pacopaco Aug 17 '17 at 8:03
  • 14
    @Pacopaco: You cannot :-). You can override builtin, too: function builtin { echo "Hi from builtin :-)"; }. Then builtin no longer works. – sleske Aug 17 '17 at 8:39
  • 12
    @Pacopaco, type builtin will tell you whether it's the real builtin. Unless type has been replaced with a non-builtin type... – Toby Speight Aug 17 '17 at 11:02
  • 10
    oh... unfortunately one can : alias unset="echo unset is overridden" ; alias alias="echo alias is overridden" ... So no luck here. – Olivier Dulac Aug 17 '17 at 11:20
  • 8
    I love this ongoing discussion! – Sam Weaver Aug 17 '17 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.