I can't find any official documentation that refers to it as one or the other. What, exactly, is the technical distinction between a command and an operator, anyway?

  • 2
    I don't think it's either; is there something you've encountered that suggests otherwise? – Michael Homer Aug 28 '18 at 5:03
  • Command substitution is syntax and grammar. The $(...) is in itself not a keyword nor command. – Kusalananda Aug 28 '18 at 6:14
  • @StevieD : From my interpretation of the bash man page, the only operators outside a specific context such as [[....]], ((...)) or ${....} - these contexts define their own set of operators - are the so-called control operators, which are: || & && ; ;; ;& ;;& ( ) | |& <newline>. Hence, $() itself is not an operator. – user1934428 Aug 28 '18 at 7:39
  • So if it's not either, what do you call it? A token? – StevieD Aug 28 '18 at 13:30
  • 1
    It matters, sure. It's similar to the reason why we diagram a sentence. Being able to place labels and categorize parts of speech enhances your ability more accurately describe how meaning is derived from a sentence. – StevieD Aug 29 '18 at 14:06

A command:

  • usually has an alphanumeric string for its name.

  • is either an external util, a builtin, or a function. Most anything the type builtin produces output for is a command in that sense.

  • has prefix notation.

  • usually changes some kind of I/O.

An operator:

  • usually is a symbolic string, and may have no name, but might have a nickname.

  • is not understood by the type builtin, (unless perhaps there's some confusingly named function or script).

  • more often has infix or suffix syntax.

  • sometimes directs I/O like a traffic cop, but doesn't change what's in it.

  • sometimes directs the order of processes.

$() is a command substitution:

  • first of all, (or last of all, depending how we look at it), it's a string.
  • the string is made up of the text output of those commands listed within.

[ is a shell builtin command:

  • it's the test command with a mandatory ] suffix.

[[ is a bash shell keyword:

  • It's like a more versatile and faster [ builtin, but SFAIK with no corresponding test-like command.
  • shell keywords include things like time and for, that look like commands but behave more like operators that require various sibling keywords as delimiters and syntax. This code runs the type builtin on the output of the help builtin, which shows a list of left-side keywords and builtins:

    COLUMNS=30 help -m | tail -n +9 | 
    tee >(cut -d ' ' -f2) >(cut -c 17-30 | cut -d ' ' -f1) > /dev/null | 
    sort | sed -n "s/.*/type '&'/e"';/found/!p'

Comparing it to a spoken language:

  • commands (and some keywords) are like verbs.
  • files and strings are nouns.
  • operators are like punctuation and prepositions.

In the abstract, operators are just commands with different syntax that could be replaced with workalike prefix style commands along with the rest of them. It probably wouldn't be as convenient to use.

Analytically, the compiled code of a shell could be run through a disassembler, and the operators are again just commands, or rather a series of assembly language commands.

  • A keyword is not a shell command (syntatic analysis). The classification of the commands is incorrect (see POSIX specification). $(...) is not a string substitution but a command substitution. Finally, the comparison with a sentence is strange. – Fólkvangr Aug 29 '18 at 16:22
  • @Fólkvangr, Thanks! Corrected the $() error. On keywords, in bash, run type '[['; the output is [[ is a shell keyword. Pity you don't like it, but I must defend to the death the comparison with a sentence, which is borrowed/adapted from Mathematics for the Million. – agc Aug 29 '18 at 18:29
  • Your answer is based on an empirical classification (missing fundamental structures, c.f. POSIX specification). See also, Bash Reference Manual, chapter "Definitions". – Fólkvangr Aug 29 '18 at 19:00
  • @Fólkvangr, True, this answer is empirical-folksy. But command line shells evolved in bits and pieces as well. Retconning some ideal structure or standard on it, (even if we like it better, or if it is better), can't change that, anymore than padding a resume improves worker skills. Anyway, good comments. Please consider posting a better answer, I'd upvote it. – agc Aug 29 '18 at 20:06

The text in POSIX lists it under 2.6 Word Expansions, along with Tilde Expansion, Parameter Expansion, Arithmetic Expansion, Field Splitting, Pathname Expansion and Quote Removal.

Similarly Bash's manual lists it under Shell Expansions.

The former uses "operator" for redirection operators, and ;, &, |, &&, ||, ( etc. so it's not one of them. It's also obviously not a command, so as much or little as the terminology matters, I'd have to say it's neither of those.

  • $(...) is a kind of shell primitive (c.f. Bash Reference Manual, chapter "Introduction", section "What is a shell?"). How does the shell achieve a command substitution? – Fólkvangr Aug 29 '18 at 15:16
  • @Fólkvangr, umh, this page? 1.2 What is a shell? I can't see any mention of command substitution there, or "shell primitives" for that matter. And since we're discussing terminology, I can't see the phrase "shell primitive" anywhere in Bash's manual, and the word "primitive" only appears once, in the phrase "Primitives used in composing expressions for the test builtin." which links to section 6.4 Bash Conditional Expressions, so I rather fail to see how that terminology applies. – ilkkachu Aug 29 '18 at 17:37
  • $(...) is an atomic element of an expression of the shell programming language. A command substitution allows the output of a command to replace the shell expression (shell expansion). – Fólkvangr Aug 29 '18 at 19:26
  • @Fólkvangr, Yes, I know what it is. I also just mentioned it's listed under expansions, even though I didn't explicitly say it is one. But I'm not sure if calling it a "language primitive" makes the comparison to operators and commands any clearer, especially since it would seem that most the operators and keywords are also primitives. But do feel free to post your own answer if you have a different view. – ilkkachu Aug 29 '18 at 19:45

From the user perpective, a command substitution is a kind of shell primitive. It is a feature implemented in the shell that performs several actions (launches a subshell, execute a command...) and returns a value (the output of the command). Before the shell performs expansions, a command substitution is usually the argument of a command. In other words, the user gets the result of a shell process using a shell feature.

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.