1
  1. For an external command which takes a command as its argument, must the argument be an external command, not a shell's builtin command?

    I ask this question, based on the following two examples

    the external command time can only time an external command.

    the external command which can only show info about an external command.

  2. If a builtin command can take a command as its argument, can the argument be either a builtin command or an external command?

If not, please provide counterexamples besides explanations.

2

Shell built-ins are only accessible to the shell, so external commands can not run them. e.g. the external time command can only run other external commands. External commands can not run shell built-ins, shell aliases, or shell functions.

Note that shells are themselves external commands (e.g. /bin/sh, /bin/bash etc), so external commands can run the shell with -c, e.g. compare sh -c 'time echo foo' to sh -c '/usr/bin/time echo foo', or execute a shell script which uses built-ins.

If there happen to be external commands with the same name as a shell built-in (e.g. time, echo, kill, ...) then external commands can run those, but even if they perform the same basic task they are likely to be slightly different (e.g. different command line options, different output format, different capabilities - built-in kill can kill by shell job number, external kill can't).

Shell built-ins (e.g. time or command) can execute either built-in or external commands, as well as shell aliases and functions.

  • thanks. About your last sentence, time isn't buitlin, but keyword in bash. – Tim Mar 17 '16 at 19:08
0

One example is the command command , it can take both internal and external commands as argument.

command
command [-pVv] command [arguments …]
Runs command with arguments ignoring any shell function named command. Only shell builtin commands or commands found by searching the PATH are executed. If there is a shell function named ls, running ‘command ls’ within the function will execute the external command ls instead of calling the function recursively. The -p option means to use a default value for PATH that is guaranteed to find all of the standard utilities. The return status in this case is 127 if command cannot be found or an error occurred, and the exit status of command otherwise.

If either the -V or -v option is supplied, a description of command is printed. The -v option causes a single word indicating the command or file name used to invoke command to be displayed; the -V option produces a more verbose description. In this case, the return status is zero if command is found, and non-zero if not.

The enable or help commands take as parameter only builtin commands.

The time command can take shell built-ins as well as argument.

  • Thanks. (1) command is a bash builtin command. It is not a counterexample to my part 2. (2) Are there buitlin commands that take only external commands as arguments? (3) Is there some rule about what kind of builtins that can take either builtin or external as argument, and what kind of builtins that can take only builtin as argument? (4) are there counterexamples to my part 1? – Tim Mar 16 '16 at 9:02
  • The external time command can't take builtin. unix.stackexchange.com/questions/269795/… – Tim Mar 16 '16 at 14:51

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.