Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is it true that all commands that can run in bash are not actually part of bash? I'm gradually realizing I have been confusing the shell and the applications that can run in it.

share|improve this question
3  
The question is unclear. If all commands aren't part of bash, then there is no rest. –  user unknown Jul 21 '11 at 1:57
    
@user: The rest are those that are not commands. Such as pipe, and redirection. –  Tim Jul 21 '11 at 2:03
    
The rest of what? Keywords? You think, redirection isn't part of the bash? Commands aren't? Everything, which isn't an external program in the path is part of the bash. What else? –  user unknown Jul 21 '11 at 2:30
    
I said the rest are part of bash. The rest also include "for", variables, expressions, ... –  Tim Jul 21 '11 at 2:41

3 Answers 3

up vote 22 down vote accepted

There isn't a sharp border between the shell and the applications.

Some of the commands that you run in a shell have to be built into the shell, because they act on the shell process. For example, cd changes the shell's working directory, and this can't be done from the outside, so cd has to be a built-in command. But this is an implementation detail. A casual user doesn't need to know that there are technical reasons that compel cd to be a built-in command.

At another extreme, there are large applications that are made by different authors, such as Firefox or Emacs. These aren't going to be built into any shell because they're too big. But this isn't a fundamental impossibility, just a matter of design. For examples, there are shells¹ that have builtins to make simple GUI applications.

In the middle, there are commands that could go either way. For example, the echo command doesn't need to be built into the shell, but almost every shell has it built in because it's very small, and is used often so should be efficient. Another example is kill, which for casual usage could be an external command, but having a built-in has several advantages: you can invoke it even if you've reached a limit on the number of processes, and you can give it a shell job number (kill %2) in lieu of a process ID. Even the [ … ] construct (which can also be written test) could, in principle, be an external command, but is built into shells for the same reason as echo.

If you're curious, you can check the status of a given command with the type command. For example, in my setup:

% type while type setenv cp emacs
while is a reserved word
type is a shell builtin
setenv is a shell function
cp is an alias for cp -i
emacs is /usr/bin/emacs

Reserved words such as while and then are part of the shell syntax. Builtins are commands that are built into the shell and don't require an external executable. Functions are compound commands that are defined and named by the user, and can be called by their name. Aliases are user-defined short names for longer commands (behaving differently from functions). The last example is an external command.

¹ dtksh. There is no free implementation.

share|improve this answer
1  
You will notice that /bin/[ exists, and is a hard link to /bin/test on most (all?) *nix boxes. It wasn't a builtin to the classic Bourne shell. There are also confusing animals such as busybox that implement most of core commands and a shell, deciding which they are at run time by inspecting the name used to invoke them. –  RBerteig Jul 21 '11 at 3:38
    
For commands like echo which are both builtin and external, use type -a to show both (in order of precedence). –  Philomath Jul 21 '11 at 5:21
  • Most commands aren't part of the shell. (Shell just runs the binary by that name.)
  • Shell implements cd and it's scripting language keywords (while, for, etc.)
  • Commands such as echo and test may or may not be implemented in shell.
share|improve this answer

Try using the bash command type. type describes what a command is: if it is an shell builtin, if it's an alias, if it's another program...

Example:

[braga@coleman ~]$ type echo
echo is a shell builtin
[braga@coleman ~]$ type ls
ls is aliased to `ls --color=auto'
[braga@coleman ~]$ type usermod
usermod is /usr/sbin/usermod

Some are builtin, some are not. Use type if you want to query about a specific command. Refer to bash documentation for a list of all builtins.

  • What is a builtin?

Builtin commands are contained within the shell itself. When the name of a builtin command is used as the first word of a simple command , the shell executes the command directly, without invoking another program.

  • What is an alias?

Aliases allow a string to be substituted for a word when it is used as the first word of a simple command. The shell maintains a list of aliases that may be set and unset with the alias and unalias builtin commands.

Both quotes come from the bash mannual.

share|improve this answer
    
Thanks! By builtin, are they applications that are bundled with bash? Are they shell features of bash? How about alias then? –  Tim Jul 20 '11 at 23:37
    
@Tim I expanded the answer to include definitions for alias and builtins. –  Vitor Jul 20 '11 at 23:41
    
Note that many builtins are also available as external commands, probably in /usr/bin or /bin. This is largely for historical reasons; the classic Bourne shell had far fewer builtins than bash does. –  RBerteig Jul 21 '11 at 3:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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