I would like to know whether the commands that we call in the shell are functions or programs.
- 1What distinction are you making between "small program" and "function"?JdeBP– JdeBP2018-09-13 12:10:43 +00:00Commented 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– txtechhelp2018-09-14 01:32:39 +00:00Commented Sep 14, 2018 at 1:32
3 Answers
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 - 3Also, a command that's a program in one implementation of *nix might well become a shell builtin (= function) in another.jamesqf– jamesqf2018-09-13 15:14:13 +00:00Commented 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– ilkkachu2018-09-13 17:16:47 +00:00Commented Sep 13, 2018 at 17:16 - 5Note that in general,
typeis preferable towhichfor almost all purposes. See unix.stackexchange.com/q/85249/135943Wildcard– Wildcard2018-09-13 18:00:39 +00:00Commented 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– jamesqf2018-09-14 17:05:31 +00:00Commented 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– ilkkachu2018-09-14 18:46:27 +00:00Commented Sep 14, 2018 at 18:46
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.
- 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– ilkkachu2018-09-13 11:51:57 +00:00Commented Sep 13, 2018 at 11:51
- @ilkkachu. Thank you. I have updated the answer. Please feel free to revise ;-)user88036– user880362018-09-13 11:56:42 +00:00Commented Sep 13, 2018 at 11:56
- 3I 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.Toby Speight– Toby Speight2018-09-13 16:51:27 +00:00Commented 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()orsetenv()in the shell that is, to them, a distinct parent process.Charles Duffy– Charles Duffy2018-09-13 17:51:31 +00:00Commented Sep 13, 2018 at 17:51
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:
- an
alias, - a function,
- a built-in command,
- an executable file.