What is the difference between the terms âprogramâ, âcommandâ and âfunctionâ in Linux & Unix?
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I would like to know whether the commands that we call in the shell are functions or programs?
shell command-line function bash-functions
New contributor
add a comment |Â
up vote
6
down vote
favorite
I would like to know whether the commands that we call in the shell are functions or programs?
shell command-line function bash-functions
New contributor
1
What distinction are you making between "small program" and "function"?
â JdeBP
5 hours ago
What practical difference do you see this making?
â Jeff Schaller
4 hours ago
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I would like to know whether the commands that we call in the shell are functions or programs?
shell command-line function bash-functions
New contributor
I would like to know whether the commands that we call in the shell are functions or programs?
shell command-line function bash-functions
shell command-line function bash-functions
New contributor
New contributor
edited 10 mins ago
Benjamin W.
393110
393110
New contributor
asked 6 hours ago
Lion
403
403
New contributor
New contributor
1
What distinction are you making between "small program" and "function"?
â JdeBP
5 hours ago
What practical difference do you see this making?
â Jeff Schaller
4 hours ago
add a comment |Â
1
What distinction are you making between "small program" and "function"?
â JdeBP
5 hours ago
What practical difference do you see this making?
â Jeff Schaller
4 hours ago
1
1
What distinction are you making between "small program" and "function"?
â JdeBP
5 hours ago
What distinction are you making between "small program" and "function"?
â JdeBP
5 hours ago
What practical difference do you see this making?
â Jeff Schaller
4 hours ago
What practical difference do you see this making?
â Jeff Schaller
4 hours ago
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
9
down vote
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 "%dn" $?
0
$ alias true=false
$ true ; printf "%dn" $?
1
$ true ; printf "%dn" $?
0
1
Also, a command that's a program in one implementation of *nix might well become a shell builtin (= function) in another.
â jamesqf
2 hours ago
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
46 mins ago
Note that in general,type
is preferable towhich
for almost all purposes. See unix.stackexchange.com/q/85249/135943
â Wildcard
3 mins ago
add a comment |Â
up vote
6
down vote
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.
5
"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
6 hours ago
@ilkkachu. Thank you. I have updated the answer. Please feel free to revise ;-)
â Goro
6 hours ago
2
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.
â Toby Speight
1 hour ago
...*mostly* alike; builtins can change the state of the shell itself, whereas external programs can't callchdir()
orsetenv()
in the shell that is, to them, a distinct parent process.
â Charles Duffy
12 mins ago
add a comment |Â
up vote
1
down vote
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.
1
About 4. Figuring out if the executable is a binary program or a script is outside the scope of the calling shell. This task is handled by the kernel.
â jlliagre
42 mins ago
@jlliagre thanks. Will edit the answer.
â RonJohn
27 mins ago
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
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 "%dn" $?
0
$ alias true=false
$ true ; printf "%dn" $?
1
$ true ; printf "%dn" $?
0
1
Also, a command that's a program in one implementation of *nix might well become a shell builtin (= function) in another.
â jamesqf
2 hours ago
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
46 mins ago
Note that in general,type
is preferable towhich
for almost all purposes. See unix.stackexchange.com/q/85249/135943
â Wildcard
3 mins ago
add a comment |Â
up vote
9
down vote
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 "%dn" $?
0
$ alias true=false
$ true ; printf "%dn" $?
1
$ true ; printf "%dn" $?
0
1
Also, a command that's a program in one implementation of *nix might well become a shell builtin (= function) in another.
â jamesqf
2 hours ago
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
46 mins ago
Note that in general,type
is preferable towhich
for almost all purposes. See unix.stackexchange.com/q/85249/135943
â Wildcard
3 mins ago
add a comment |Â
up vote
9
down vote
up vote
9
down vote
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 "%dn" $?
0
$ alias true=false
$ true ; printf "%dn" $?
1
$ true ; printf "%dn" $?
0
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 "%dn" $?
0
$ alias true=false
$ true ; printf "%dn" $?
1
$ true ; printf "%dn" $?
0
answered 6 hours ago
phg
557415
557415
1
Also, a command that's a program in one implementation of *nix might well become a shell builtin (= function) in another.
â jamesqf
2 hours ago
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
46 mins ago
Note that in general,type
is preferable towhich
for almost all purposes. See unix.stackexchange.com/q/85249/135943
â Wildcard
3 mins ago
add a comment |Â
1
Also, a command that's a program in one implementation of *nix might well become a shell builtin (= function) in another.
â jamesqf
2 hours ago
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
46 mins ago
Note that in general,type
is preferable towhich
for almost all purposes. See unix.stackexchange.com/q/85249/135943
â Wildcard
3 mins ago
1
1
Also, a command that's a program in one implementation of *nix might well become a shell builtin (= function) in another.
â jamesqf
2 hours ago
Also, a command that's a program in one implementation of *nix might well become a shell builtin (= function) in another.
â jamesqf
2 hours ago
1
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
46 mins ago
@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
46 mins ago
Note that in general,
type
is preferable to which
for almost all purposes. See unix.stackexchange.com/q/85249/135943â Wildcard
3 mins ago
Note that in general,
type
is preferable to which
for almost all purposes. See unix.stackexchange.com/q/85249/135943â Wildcard
3 mins ago
add a comment |Â
up vote
6
down vote
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.
5
"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
6 hours ago
@ilkkachu. Thank you. I have updated the answer. Please feel free to revise ;-)
â Goro
6 hours ago
2
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.
â Toby Speight
1 hour ago
...*mostly* alike; builtins can change the state of the shell itself, whereas external programs can't callchdir()
orsetenv()
in the shell that is, to them, a distinct parent process.
â Charles Duffy
12 mins ago
add a comment |Â
up vote
6
down vote
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.
5
"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
6 hours ago
@ilkkachu. Thank you. I have updated the answer. Please feel free to revise ;-)
â Goro
6 hours ago
2
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.
â Toby Speight
1 hour ago
...*mostly* alike; builtins can change the state of the shell itself, whereas external programs can't callchdir()
orsetenv()
in the shell that is, to them, a distinct parent process.
â Charles Duffy
12 mins ago
add a comment |Â
up vote
6
down vote
up vote
6
down vote
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.
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.
edited 1 hour ago
answered 6 hours ago
Goro
1,68541644
1,68541644
5
"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
6 hours ago
@ilkkachu. Thank you. I have updated the answer. Please feel free to revise ;-)
â Goro
6 hours ago
2
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.
â Toby Speight
1 hour ago
...*mostly* alike; builtins can change the state of the shell itself, whereas external programs can't callchdir()
orsetenv()
in the shell that is, to them, a distinct parent process.
â Charles Duffy
12 mins ago
add a comment |Â
5
"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
6 hours ago
@ilkkachu. Thank you. I have updated the answer. Please feel free to revise ;-)
â Goro
6 hours ago
2
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.
â Toby Speight
1 hour ago
...*mostly* alike; builtins can change the state of the shell itself, whereas external programs can't callchdir()
orsetenv()
in the shell that is, to them, a distinct parent process.
â Charles Duffy
12 mins ago
5
5
"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
6 hours ago
"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
6 hours ago
@ilkkachu. Thank you. I have updated the answer. Please feel free to revise ;-)
â Goro
6 hours ago
@ilkkachu. Thank you. I have updated the answer. Please feel free to revise ;-)
â Goro
6 hours ago
2
2
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.
â Toby Speight
1 hour ago
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.
â Toby Speight
1 hour ago
...*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.â Charles Duffy
12 mins ago
...*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.â Charles Duffy
12 mins ago
add a comment |Â
up vote
1
down vote
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.
1
About 4. Figuring out if the executable is a binary program or a script is outside the scope of the calling shell. This task is handled by the kernel.
â jlliagre
42 mins ago
@jlliagre thanks. Will edit the answer.
â RonJohn
27 mins ago
add a comment |Â
up vote
1
down vote
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.
1
About 4. Figuring out if the executable is a binary program or a script is outside the scope of the calling shell. This task is handled by the kernel.
â jlliagre
42 mins ago
@jlliagre thanks. Will edit the answer.
â RonJohn
27 mins ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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.
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.
edited 27 mins ago
answered 1 hour ago
RonJohn
495213
495213
1
About 4. Figuring out if the executable is a binary program or a script is outside the scope of the calling shell. This task is handled by the kernel.
â jlliagre
42 mins ago
@jlliagre thanks. Will edit the answer.
â RonJohn
27 mins ago
add a comment |Â
1
About 4. Figuring out if the executable is a binary program or a script is outside the scope of the calling shell. This task is handled by the kernel.
â jlliagre
42 mins ago
@jlliagre thanks. Will edit the answer.
â RonJohn
27 mins ago
1
1
About 4. Figuring out if the executable is a binary program or a script is outside the scope of the calling shell. This task is handled by the kernel.
â jlliagre
42 mins ago
About 4. Figuring out if the executable is a binary program or a script is outside the scope of the calling shell. This task is handled by the kernel.
â jlliagre
42 mins ago
@jlliagre thanks. Will edit the answer.
â RonJohn
27 mins ago
@jlliagre thanks. Will edit the answer.
â RonJohn
27 mins ago
add a comment |Â
Lion is a new contributor. Be nice, and check out our Code of Conduct.
Lion is a new contributor. Be nice, and check out our Code of Conduct.
Lion is a new contributor. Be nice, and check out our Code of Conduct.
Lion is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f468764%2fwhat-is-the-difference-between-the-terms-program-command-and-function-in%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
What distinction are you making between "small program" and "function"?
â JdeBP
5 hours ago
What practical difference do you see this making?
â Jeff Schaller
4 hours ago