Skip to main content

Operators And Tokens

Laurence MorganAbout 4 minUser Guide

Operators And Tokens

All supported operators and tokens

Syntax

Murex supports both expressions and statements. You can use the interchangeably with your code and the Murex parser will decide whether to run that code as an expression or statement.

Expressions

Expressions are patterns formed like equations (eg $val = 1 + 2).

Strings must be quoted in expressions.

Statements

Statements are traditional shell command calls (eg command parameters...).

Quoting strings is optional in statements.

Not all operators are supported in statements.

Order Of Operations

Expressions and statements are split by pipes and terminators. Each statement and expression is executed from left to right, with the statement or expression parsed by the following rules of operation

Order of operations:

  1. expression or statement discovery
  2. sub-shells / sub-expressions
  3. multiplication / division (expressions only)
  4. addition / subtraction (expressions only)
  5. immutable merge
  6. comparisons, eg greater than (expressions only)
  7. logical and (sub-expressions only)
  8. logical or (sub-expressions only)
  9. elvis (expressions only)
  10. assign (expressions only)
  11. left to right

Expression Or Statement Discovery

First a command is read as an expression. Because the rules of parsing expressions are stricter than statements, everything is assumed to be an expression unless the expression parser fails, which then it is assumed to be a statement.

Operators And Tokens

Terminology

  • left: this is the value to the left hand side of the operator
  • right: this is the value to the right hand side of the operator

Example: left operator right

Modifiers

All modifiers replace the left, operator and right with the returned value of the modifier.

All returns will be num data type (or their original type if strict types is enabled).

Modifiers are only supported in expressions.

OperatorNameOperation
*MultiplicationMultiply left by right
/DivideDivide left by right
+AdditionAdd left with right
-SubtractionSubtract left by right

Read more:

Immutable Merge

Returns the result of merging right into left.

immutable merge does not modify the contents of either left nor right.

The direction of the arrow indicates that the result returned is a new value rather than an updated assignment.

Left can be a statement or expression, whereas right can only be an expression. However you can still use a sub-shell as part of, or the entirety, of, that expression.

OperatorNameOperation
~>Immutable MergeReturns merged value of right into left

Comparators

All comparators replace the left, operator and right with the returned value of the comparator.

All returns will be bool data type, either true or false.

Comparators are only supported in expressions.

OperatorNameOperation
>Greater Thantrue if left is greater than right
>=Greater Or Equal Totrue if left is greater or equal to right
<Less Thantrue if left is less than right
<=Less Or Equal Totrue if left is less or equal to right
==Equal Totrue if left is equal to right
!=Not Equal Tofalse if left is equal to right
~~Liketrue if left string is like right string
!!Not Likefalse if left string is like right string
=~Matches Regexptrue if left matches regexp pattern on right
!~Does Not Match Regexpfalse if left matches regexp pattern on right

Read more:

Assignment

Assignment returns null if successful.

Assignment is only supported in expressions.

OperatorNameOperation
=Assign (overwrite)Assign right to left
:=Assign (retain)EXPERIMENTAL
<~Assign Or MergeMerge right (array / object) into left
+=Assign And AddAdd right to left and assign to left
-=Assign And SubtractSubtract right from left and assign to left
*=Assign And MultiplyMultiply right with left and assign to left
/=Assign And DivideDivide right with left and assign to left
++Add one to variableAdds one to right and reassigns
--Subtract one from varSubtracts one from right and reassigns

Read more:

Conditionals

Conditionals replace left, operator and right with the value defined in operation.

These conditionals are only supported in expressions.

OperatorNameOperation
??Null CoalescenceReturns left if not null, otherwise right
?:ElvisReturns left if truthy, otherwise right

Read more:

Sigils

Sigils are special prefixes that provide hints to the parser.

Sigils are supported in both expressions and statements.

TokenNameOperation
$ScalarExpand value as a string
@ArrayExpand value as an array
~HomeExpand value as the persons home directory
%BuilderCreate an array, map or nestable string

Constants

Constants are supported in both expressions and statements. However null, true, false and number will all be interpreted as strings in statements.

TokenNameOperation
nullNullnull (null / nil / void) type
trueTruebool (boolean) true
falseFalsebool (boolean) false
numberNumbernum (numeric) value
'string'String Literalstr (string) literal value
"string"Infix Stringstr (string) value, supports escaping & infixing
{ code }Code Blockstr (string) value, surrounded by curly braces
%( string )String BuilderCreates a nestable str (string)
%[ array ]Array BuilderCreates a json (JSON) array (list)
%{ map }Object BuilderCreates a json (JSON) object (map / dictionary)

Read more:

Sub-shells

Sub-shells are a way of inlining expressions or statements into an existing expression or statement. Because of this they are supported in both.

SyntaxNameOperation
command( parameters... )C-Style FunctionsInline a command as a function
${command parameters...}Sub-shell (scalar)Inline a command line as a string
@{command parameters...}Sub-shell (array)expand a command line as an array
(expression)Sub-expressionInline an expression (statement)
(expression)Sub-expressionOrder of evaluation (expression)
[{ code block }]LambdaRun operations across lists / maps

Read more:

Boolean Operations

Boolean operators behave like pipes.

They are supported in both expressions and statements.

OperatorNameOperation
&&AndEvaluates right if left is truthy
||OrEvaluates right if left is falsy

Pipes

Pipes always flow from left to right.

They are supported in both expressions and statements.

OperatorNameOperation
|POSIX PipePOSIX compatibility
->Arrow PipeContext aware pipe
=>Generic PipeConvert stdout to * (generic) then pipe
|>Truncate FileWrite stdout to file, overwriting contents
>>Append FileWrite stdout to file, appending contents

Terminators

"LF" refers to the life feed character, which is a new line.

TokenNameOperation
;Semi-ColonEnd of statement or expression (optional)
LFLine FeedEnd of statement or expression (new line)

Escape Codes

Any character can be escaped via \ to signal it isn't a token. However some characters have special meanings when escaped.

"LF" refers to the life feed character, which is a new line.

TokenNameOperation
\sSpaceSame as a space character
\tTabSame as a tab character
\rCarriage ReturnCarriage Return (CR) sometimes precedes LF
\nLine FeedLine Feed (LF), typically a new line
\LFEscaped Line FeedStatement continues on next line

See Also

  • Expressions (expr): Expressions: mathematical, string comparisons, logical operators
  • Language Tour: Getting started with Murex: a quick tour of the next generation of shell scripting

This document was generated from gen/user-guide/operators-tokens_doc.yamlopen in new window.

Last update:
Contributors: Laurence Morgan