Rosetta Stone
About 4 minGuide
Rosetta Stone
A tabulated list of Bashism's and their equivalent Murex syntax
Below is a reference table of common Bash code and how it could be written in Murex.
It is also recommended that you read the language tour if you want to learn more about shell scripting in Murex.
Description | Bash | Murex |
---|---|---|
Write to STDOUT | echo "Hello Bash" | out "Hello Murex" echo "Hello Murex" [1] |
Write to STDERR | echo "Hello Bash" >2 | err "Hello Murex" |
Write to file (truncate) | echo "Hello Bash" > hello.txt | echo "Hello Murex" |> hello.txt |
Write to file (append) | echo "Hello Bash" >> hello.txt | echo "Hello Murex" >> hello.txt |
Pipe commands | echo "Hello Bash | grep Bash | echo "Hello Murex | grep Murex out "Hello Murex" -> regexp m/Murex/ |
Redirect errors to STDOUT | curl murex.rocks 2>&1 | less | curl murex.rocks ? less curl <!out> murex.rocks | less |
Redirect output to STDERR | uname -a >&2 | uname <err> -a |
Quoting strings | ||
Infixing | echo "Hello $SHELL" | out "Hello $SHELL" |
String literals | echo 'Hello' $SHELL | out 'Hello' $SHELL |
Nesting quotes | echo 'Hello \'Bob\'' | out %(Hello 'Bob') |
Process management | ||
Exit number | $? | exitnum |
Background jobs | command & | bg { command } |
Job control | ps ,jobs ,bg pid ,fg pid | fid-list ,jobs ,bg fid ,fg fid |
Happy paths | command && command | command && command try {command; command} |
Unhappy paths | command || command | command || command try {command}; catch {command} |
Pipe fail | set -o pipefail | runmode trypipe module runmode trypipe function trypipe { commands } |
Comments | ||
Single line | # comment | # comment |
Multiple lines | :<<EOC line 1 line 2 EOC | /# line 1 line 2 #/ |
Mid-line | n/a | eg out foo/#comment#/bar |
File pattern matching (also known as "wildcards") | ||
Globbing | eg ls *.txt | eg ls *.txt (in the interactive terminal) g pattern eg ls @{g *.txt} |
Regexp | n/a | rx pattern eg ls @{rx '*\\.txt'} |
File type matching | n/a | f flags eg f +s (only return symlinks) |
Chaining | n/a | eg f +f | g *.txt | !g murex.* (returns only files with the extension "txt" that aren't called "murex") |
Expressions | ||
Assignment | foobar = $((1 + 2 * 3)) | foobar = 1 + 2 * 3 [2] |
Comparison, string | [ "$(command)" == "value" ] | ${command} == "value" [2] [5] |
Comparison, numeric | [ $integer -eq 5 ] | $number == 5 [2] |
Arithmetic | echo $(( 1+2*3 )) | 1 + 2 * 3 [2] out ${1+2*3} [2] [5] |
Supported data types | 1. String, 2. Integer (all variables are strings) | 1. String, 2. Integer, 3. Float (default number type), 4. Boolean 5. Array, 6. Object, 7. Null (all variables can be treated as strings and/or their primitive) |
Variables | ||
Assign a local variable | local foo="bar" | $foo = "bar" [2] [6]out "bar" | set foo |
Assign a global variable | foo="bar" | $GLOBAL.foo = "bar" [6]out "bar" | global foo |
Assign an environmental variable | export foo="bar" | export foo = "bar" [1] [2] [3]$ENV.foo = "bar" [6]out "bar" | export foo [3] |
Printing a variable | echo "$foobar" | out $foobar [5]$foobar (variables don't need to be quoted in Murex) |
Arrays (eg arrays, lists) | ||
Creating an array | array_name=(value1 value2 value3) | %[value1 value2 value3] %[value1, value2, value3] eg array_name = %[1, 2, 3] , eg %[hello world] | foreach { ... } |
Accessing an array element | ${array_name[0]} | $array_name[0] (immutable) $array_name.0 (mutable) [5] array | [0] |
Printing multiple elements | echo ${array_name[1]} ${array_name[0]} | @array_name[1 0] array | [1 0] |
Printing a range of elements | n/a | @array_name[1..3] array | [1..3] |
Printing all elements | echo ${array_name[*]} | @array_name |
Iterating through an array | for item in array; do; Â Â Â Â $item done; | array | foreach item { $item } eg %[Tom Richard Sally] | foreach name { out "Hello $name" } |
Objects (eg JSON objects, maps, hashes, dictionaries) | ||
Creating an object | n/a | %{ key: value, array: [1, 2, 3] } [2] eg object_name = %{ key: val, arr: [1,3,3] } eg %{ a:1, b:2, c:3 } | formap { ... } |
Accessing an element | n/a | $object_name[key] (immutable) $object_name.key [5] (mutable) object | [key] |
Printing multiple elements | n/a | $object_name[key1 key2] object | [key1 key2] |
Accessing a nested element | n/a | $object_name[[.path.to.element]] (immutable) [4]$object_name.path.to.element (mutable)object | [[.path.to.element]] [4] |
Iterating through an map | n/a | object | formap key value { $key; $value } eg %{Bob: {age: 10}, Richard: {age: 20}, Sally: {age: 30} } | formap name person { out "$name is $person[age] years old" } |
Sub-shells | ||
Sub-shell, string | "$(commands)" eg "echo $(echo "Hello world")" | ${commands} [5] eg out ${out Hello world} |
Sub-shell, arrays | $(commands) eg $(echo 1 2 3) | @{commands} [5] eg out @{ %[1,2,3] } |
Footnotes
- Supported for compatibility with traditional shells like Bash.
- Unlike Bash, whitespace (or the absence of) is optional.
- Environmental variables can only be stored as a string. This is a limitation of current operating systems.
- Path separator can be any 1 byte wide character, eg
/
. The path separator is defined by the first character in a path. - Murex uses
${}
for subshells and$()
for variables, the reverse of what Bash and others use. The reason for this difference is because{}
always denotes a code block and()
denotes strings. So${foobar}
makes more sense as a subshell executing the commandfoobar
, while$(foobar)
makes more sense as the variable$foobar
. - When assigning a variable where the right hand side is an expression, eg
$foo = "bar"
, the dollar prefix is optional. Theset
,global
andexport
keywords are considered deprecated.
See Also
- And (
&&
) Logical Operator: Continues next operation if previous operation passes - Append Pipe (
>>
) Token: Redirects STDOUT to a file and append its contents - Array (
@
) Token: Expand values as an array - Murex Named Pipes: A detailed breakdown of named pipes in Murex
- Or (
||
) Logical Operator: Continues next operation only if previous operation fails - String (
$
) Token: Expand values as a string - Terminal Hotkeys: A list of all the terminal hotkeys and their uses
>>
(append file): Writes STDIN to disk - appending contents if file already exists>
(truncate file): Writes STDIN to disk - overwriting contents if file already exists[[
(element): Outputs an element from a nested structure[
(index): Outputs an element from an array, map or table[
(range) : Outputs a ranged subset of data from STDINrunmode
: Alter the scheduler's behaviour at higher scoping leveltry
: Handles errors inside a block of codetrypipe
: Checks state of each function in a pipeline and exits block on error