Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Precedence of shell operator

I'm currently trying to re-code a shell in C using a BNF and LL parser. Otherwise, I need to know what is the precedence of shell operator of |, <<, ,, <, >>, >, &, ;?

Is there anyone who can provide me it ? Thank you

like image 815
S7_0 Avatar asked Aug 09 '15 13:08

S7_0


People also ask

Which operator has the highest precedence?

This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator. Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom.

What is precedence of logical operators?

Logical operators have operator precedence the same as other operators (relational, arithmetic, etc.). The highest precedence belongs to not , followed by and , and finally by or . Like other operations, grouping takes precedence, so we must evaluate bracketed expressions first, if they exist.

What is the order of precedence of operators in NOT and OR?

The order of precedence is: logical complements ( Not ) are performed first, logical conjunctions ( And ) are performed next, and logical disjunctions ( Or ) are performed at the end.


1 Answers

  1. Redirections (<, >, >>, <>, <&. >& and >>-, as well as here-docs <<delimiter and here-strings <<<word) are roughly the same as command-line arguments, and can appear anywhere in a simple command, including before the command word. Effectively, they bind most tightly, as with postfix operators in most languages.
  2. Pipes (|) are the strongest binary operator. They associate to the left.
  3. Finally come the short-circuiting booleans (&& and ||). Unlike many languages, these have the same precedence. They also associate to the left.

, is not a bash operator. ; and & are statement terminators, not separators, although in some circumstances the final separator is optional. Effectively, they have the lowest precedence.

See the shell grammar for details. There are lots of details.

like image 183
rici Avatar answered Dec 14 '22 23:12

rici