0

Are there any meaningfull difference between a command and an expression in bash scripting? Also, I know (command) opens a new subshell and runs the command, but what i dont know is what happens when you have (expression). What happens when you an expression instead of a command.

eg

[[ ( "$#" < 1 ) ]] 

Thanks

6
  • Unless you have a command called 2+3, you will get a "command not found" error. I don't think I fully understand the question.
    – Kusalananda
    Commented Oct 26, 2022 at 10:49
  • @Kusalananda I sometimes see if (2+3) then..... question is does (expresion) behave differently in conditional tests? Commented Oct 26, 2022 at 10:51
  • In what way is the example in your script not using a command within (...)? It looks like a command to me. A compound command consisting of an OR-list and two uses of the [ utility, to be precise.
    – Kusalananda
    Commented Oct 26, 2022 at 10:55
  • @Kusalananda i see it now Commented Oct 26, 2022 at 10:56
  • Ok, you keep changing the example. It's difficult to write an answer. Will you hold it for a bit?
    – Kusalananda
    Commented Oct 26, 2022 at 10:58

1 Answer 1

2
[[ ( "$#" < 1 ) ]] 

This is a [[ ... ]] test that checks whether $#, the number of positional parameters, sort before 1 lexicographically (use -lt in place of < to perform an arithmetic "less than" test).

Within the special [[ ... ]] syntax in the bash shell, the parentheses do not introduce a sub-shell but rather provide a way of grouping expressions in order to override the normal precedence of operators.

This is explained in the bash manual, where it describes the [[ ... ]] syntax:

[[ expression ]]

[...]

Expressions may be combined using the following operators, listed in decreasing order of precedence:

( expression )
Returns the value of expression. This may be used to override the normal precedence of operators.
[...etc...]

1
  • Thanks a lot...its now very clear.... Commented Oct 26, 2022 at 11:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .