0

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?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
StevieD
  • 915
  • 2
    I don't think it's either; is there something you've encountered that suggests otherwise? – Michael Homer Aug 28 '18 at 05:03
  • Command substitution is syntax and grammar. The $(...) is in itself not a keyword nor command. – Kusalananda Aug 28 '18 at 06: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 07:39
  • So if it's not either, what do you call it? A token? – StevieD Aug 28 '18 at 13:30
  • @MichaelHomer, I have have seen tutorial refer to it as an "operator" but I didn't think that was correct. I don't think it is a "command" either but I don't know what else to call it. I'd like to know how to properly refer to this piece of syntax. – StevieD Aug 28 '18 at 13:32
  • I wonder what the relevance is, does it matter with something what it's called? – ilkkachu Aug 29 '18 at 11:25
  • @ilkkachu, If it confuses students, then at the very least it matters just because of that. – agc Aug 29 '18 at 11:48
  • 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

3 Answers3

2

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.

ilkkachu
  • 138,973
  • @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
  • @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
1

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.

agc
  • 7,223
  • @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
  • @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
1

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.