19

I was looking up the difference between adduser and useradd, and an explanation is that useradd is a command while adduser is a perl script. I understand what a perl script is, but what I do not understand is what a command then exactly is.

I always thought that commands like ls, ln, cd etc are all simple programs written in whatever language that simply do one thing. What is the difference between these "simple programs" and a perl script?

I of course know that a (perl) script is not compiled but interpreted at runtime, but I guess that is not the only difference?

  • 1
    don't focus too much on compiled vs interpreted in this case. you could have an interpreted shell (though why you would want one is questionable). focus instead on the fact that the code for a commands action is a static part of the shell's source code, whereas a script is external, should not be be assumed to exist, or to behave exactly the same on two slightly different systems, and may not necessarily even have all its dependencies met. scripts are more flexible, and for that reason, less reliable in general, whereas a command will work anywhere the shell will. – Frank Thomas May 20 '15 at 13:24
  • In simple words: command is anything that can execute while program is a binary that can execute. – Pithikos May 21 '15 at 10:50
32

In simple terms, a command is an instruction (or a set of instructions) to be carried out by a computer.

Stand-alone commands

Fundamental Unix utilities such as ls, ln, etc. are (usually) written in C and compiled to be stand-alone executable programs that don’t require an interpreter to be executed; they usually require certain library files to be installed on the system but that’s an answer for another question.

Scripts

A script is a collection of commands and in fact, scripts themselves are considered to be a command.

A Perl script is a sequence of Perl statements and requires a perl executable (stand-alone and compiled) program to interpret the Perl statements.

Sometimes large and complex interpretative scripts (in languages such as Perl, Python and Ruby) are also referred to as interpreted programs while the term script is reserved for shorter and simpler scripts.

A shell script is a sequence of other commands (any type of command) and it requires a Unix shell such as Bash to interpret the script. From the Bash man page:

Bash is an sh-compatible command language interpreter that executes commands read from the standard input or from a file.

Shell Built-ins

Shells usually have built-in commands which are neither stand-alone programs nor scripts. Instead, they are part of the shell itself and run directly by the shell. cd is an example of such a built-in command.

Some times there are commands which exist as shell built-ins and as stand-alone commands at the same time, e.g., the echo command.

$ type -a echo
echo is a shell builtin
echo is /usr/bin/echo

echo on its own executes the shell built-in while the stand-alone command can be executed by providing its full path.

Run built-in version of echo:

$ echo --version
--version

Run stand-alone echo program:

$ /usr/bin/echo --version
echo (GNU coreutils) 8.23
Packaged by Cygwin (8.23-4)
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Note: The above specifics refer to a Unix environment but the same principles apply to a Windows environment.

  • The echo you refer to in windows is only if you install Cygwin. You won't find echo.* in windows as that is purely a CMD shell command (echo in powershell is aliased to write-object) – Jim B May 21 '15 at 4:01
  • @JimB That's true. I edited the last line to clarify what I meant. Coincidentally, I only just started to learn PowerShell this week (thankfully, I haven't had to work with .bat files in many, many years). – Anthony Geoghegan May 21 '15 at 8:45
2

A built in command is part of the shell. A program is executed by the shell.

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 (see Simple Commands), the shell executes the command directly, without invoking another program. Builtin commands are necessary to implement functionality impossible or inconvenient to obtain with separate utilities.

http://www.gnu.org/software/bash/manual/bashref.html#Shell-Builtin-Commands

  • But useradd isn't a shell built-in. The web site just said "command", not "built-in command". How is this explanation relevant to this question? – Barmar May 22 '15 at 19:02
1

Command just means a way to tell an application or system to do something.

An application will typically accept many different commands, either from the GUI, from stdin, but other methods are possible, e.g. a UNIX socket or named pipe, some sort of web API, an RPC connection, or some other custom protocol.

An application that does only one thing, then exits, typically without a GUI, can also be called a command, because you can really only "give" this application one meaningful "command." This is how small programs like ls and such work and why they are called commands.

But you wouldn't call Photoshop a command, but you'd certainly issue commands within it via the GUI.

However, the term can mean different things to different people. In your example, command is being used to describe an executable that is run directly, versus a file that requires a script interpreter to work. The distinction can be important because when you are running a Perl script, /usr/bin/perl is the binary that is actually running (so if you want to kill a long running Perl script, that's what you have to look for in ps). However, most shells have "built-in" commands that are commands to the shell itself and don't cause an external executable to run. For example, cd is handled by bash itself and it doesn't call /sbin/cd or similar.

  • Interesting perspective. At the level of abstraction you are viewing this from, I'm tempted to call it an 'invocation' or a 'gesture' or even to go with the android terminology an 'intent'. That said I don't know if I like the idea of making compiled vs interpreted a criteria. Powershell for instance is hybrid-interpreted (,Net), but as a shell, it has commands within its codebase. but then you make a good point about external compiled (machine code) executables being differant from scripts, but also commands. as I said, interesting perspective. – Frank Thomas May 20 '15 at 13:46

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.