9

I would like to know whether the commands that we call in the shell are functions or programs.

2
  • 1
    What distinction are you making between "small program" and "function"?
    – JdeBP
    Commented Sep 13, 2018 at 12:10
  • You can enter commands into a shell program that let you program the shell to function and take command utilizing functions that the shell was programmed with to command other programs from the shell functions.
    – txtechhelp
    Commented Sep 14, 2018 at 1:32

3 Answers 3

16

It depends.

Commands can fall into multiple categories: builtins, aliases, functions, executables (scripts and binaries in the search path).

On the command line, these occupy a single, flat namespace which makes overriding possible. There are numerous ways of telling kinds of programs apart:

$ f () { :; }
$ alias a=cat
$ which f
f ()
{ 
    :
}

We know that f is a function.

$ which a
alias a='cat'
        /usr/bin/cat

We know that a is an alias.

$ which yes
/usr/bin/yes

We know that yes is a program.

$ builtin echo ; echo $?

0

The shell has an echo builtin …

$ builtin cat ; echo $?
bash: builtin: cat: not a shell builtin
1

… but none for cat. If there is a builtin or an alias but you insist on calling the program instead, prefix the command with a backslash:

$ builtin true | printf "%d\n" $?
0
$ alias true=false
$ true ; printf "%d\n" $?
1
$ \true ; printf "%d\n" $?
0
5
  • 3
    Also, a command that's a program in one implementation of *nix might well become a shell builtin (= function) in another.
    – jamesqf
    Commented Sep 13, 2018 at 15:14
  • 1
    @jamesqf, yeah, shell's have standard tools built in, but builtin commands are not the same as functions. Run e.g. sh -c 'type true; f() { echo x; }; type f'
    – ilkkachu
    Commented Sep 13, 2018 at 17:16
  • 5
    Note that in general, type is preferable to which for almost all purposes. See unix.stackexchange.com/q/85249/135943
    – Wildcard
    Commented Sep 13, 2018 at 18:00
  • @ilkkachu: That depends on your definition of "the same as". Of course they are implemented differently, but to the ordinary user typing a command, they are functionally the same.
    – jamesqf
    Commented Sep 14, 2018 at 17:05
  • 1
    @jamesqf, even if your ordinary user doesn't (know how to) use functions, claiming they're the same as builtins is just confusing.
    – ilkkachu
    Commented Sep 14, 2018 at 18:46
3

The definition of a function is returning single values, and does not output anything. Shell functions in particular may very well and could have an output or other side effects, since the return value of functions is so limited.

A command is an instruction given by a user to tell a computer to do something, for example, executing a single program or a group of linked programs.

A program is a sequence of instructions (i.e. commands) that are given to a computer and understandable by the computer's central processing unit (CPU). these instructions indicates which operations the computer should perform on a set of data.

Having said that, functions are logical subset of the program. Calling one is entirely within your process. The command is a program (or a shell built-in) can be executed from the command shell. The command implements functions which perform a task. The opposite is not correct.

4
  • 11
    "a function returns a single value, and does not output anything" -- well, yeah, in the mathematical sense maybe. But in imperative programming languages, it's really common for "function" to mean just a subroutine, one that could do anything. And shell functions in particular may very well have output or other side effects, since the return value of functions is so limited.
    – ilkkachu
    Commented Sep 13, 2018 at 11:51
  • @ilkkachu. Thank you. I have updated the answer. Please feel free to revise ;-)
    – user88036
    Commented Sep 13, 2018 at 11:56
  • 3
    I think these definitions are from a programming-language context, not a shell context. Yes, I know the shell language is a programming language, but its idea of a "function" is quite different to the mathematical or computer-science definition - in shell, all commands (whether builtins, functions, aliases, programs or scripts) act alike, and are simply different kinds of implementation. Commented Sep 13, 2018 at 16:51
  • 1
    ...*mostly* alike; builtins can change the state of the shell itself, whereas external programs can't call chdir() or setenv() in the shell that is, to them, a distinct parent process. Commented Sep 13, 2018 at 17:51
1

I would like to know whether the commands that we call in the shell are functions or programs?

Yes.

Specifically, when you type in some text and press enter, the shell must determine whether it's:

  1. an alias,
  2. a function,
  3. a built-in command,
  4. an executable file.
0

You must log in to answer this question.

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