github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/docs/user-guide/rosetta-stone.md (about) 1 # Rosetta Stone 2 3 > A tabulated list of Bashism's and their equivalent Murex syntax 4 5 6 7 Below is a reference table of common Bash code and how it could be written in 8 Murex. 9 10 It is also recommended that you read the language [tour](../tour.md) 11 if you want to learn more about shell scripting in Murex. 12 13 <h2>Table of Contents</h2> 14 15 <div id="toc"> 16 17 - [Output \& error streams](#output--error-streams) 18 - [Quoting strings](#quoting-strings) 19 - [Process management](#process-management) 20 - [Comments](#comments) 21 - [File pattern matching](#file-pattern-matching) 22 - [Expressions](#expressions) 23 - [Variables](#variables) 24 - [Arrays](#arrays) 25 - [Objects](#objects) 26 - [Sub-shells](#sub-shells) 27 - [Common one-liners](#common-one-liners) 28 - [Footnotes](#footnotes) 29 30 </div> 31 32 33 ### Output & error streams 34 | Description | Bash | Murex | 35 |---------------|---------------|--------| 36 | [Write to STDOUT](../commands/out.md) | `echo "Hello Bash"` | `out "Hello Murex"` <br/><br/>`echo "Hello Murex"` [[1]](#footnotes)| 37 | [Write to STDERR](commands/err.md) | `echo "Hello Bash" >2` | `err "Hello Murex"` | 38 | Write to file (truncate) | `echo "Hello Bash" > hello.txt` | `echo "Hello Murex" \|> hello.txt`| 39 | Write to file (append) | `echo "Hello Bash" >> hello.txt` | `echo "Hello Murex" >> hello.txt`| 40 | [Pipe commands](../parser/pipe-arrow.md) | `echo "Hello Bash \| grep Bash` | `echo "Hello Murex \| grep Murex` <br/><br/> `out "Hello Murex" -> regexp m/Murex/` | 41 | [Redirect errors to STDOUT](../parser/pipe-err.md) | `curl murex.rocks 2>&1 \| less` | `curl <!out> murex.rocks \| less` | 42 | Redirect output to STDERR | `uname -a >&2` | `uname <err> -a` | 43 | Ignore STDERR output | `echo something 2>/dev/null` | `echo <!null> something` | 44 | Output [ANSI colors and styles](../user-guide/ansi_doc.md) | `echo -e "\n\032[0m\033[1mComplete!\033[0m\n"` | `out "{GREEN}{BOLD}Complete!{RESET}"` | 45 46 ### Quoting strings 47 | Description | Bash | Murex | 48 |---------------|---------------|--------| 49 | [Infixing](../parser/double-quote.md) | `echo "Hello $SHELL"` | `out "Hello $SHELL"` | 50 | [String literals](../parser/single-quote.md) | `echo 'Hello' $SHELL` | `out 'Hello' $SHELL` | 51 | [Nesting quotes](../parser/brace-quote.md) | `echo 'Hello \'Bob\''` | `out %(Hello 'Bob')` | 52 53 ### Process management 54 | Description | Bash | Murex | 55 |---------------|---------------|--------| 56 | [Exit number](../commands/exitnum.md) | `$?` | `exitnum` | 57 | [Background jobs](../commands/bg.md) | `command &` | `bg { command }` | 58 | [Job control](../commands/fid-list.md) | `ps`,<br/>`jobs`,<br/>`bg pid`,<br/>`fg pid` | `fid-list`,<br/>`jobs`,<br/>`bg fid`,<br/>`fg fid` | 59 | Happy paths | `command && command` | `command && command` <br/><br/> `try {command; command}` | 60 | Unhappy paths | `command \|\| command` | `command \|\| command` <br/><br/> `try {command}; catch {command}` | 61 | Pipe fail | `set -o pipefail` | `runmode trypipe module` <br/><br/> `runmode trypipe function` <br/><br/> `trypipe { commands }` 62 63 ### Comments 64 | Description | Bash | Murex | 65 |---------------|---------------|--------| 66 | Single line | `# comment` | `# comment` | 67 | Multiple lines | `:<<EOC`<br/>`line 1`<br/>`line 2`<br/>`EOC` | `/#`<br/>`line 1`<br/>`line 2`<br/>`#/` | 68 | Mid-line | n/a | eg `out foo/#comment#/bar` 69 70 ### File pattern matching 71 (also known as "wildcards") 72 | Description | Bash | Murex | 73 |---------------|---------------|--------| 74 | [Globbing](../commands/g.md) | eg `ls *.txt` | eg `ls *.txt` (in the interactive terminal) <br/><br/> `g pattern` <br/><br/> eg `ls @{g *.txt}` | 75 | [Regexp](../commands/rx.md) | n/a | `rx pattern` <br/><br/> eg `ls @{rx '*\\.txt'}` | 76 | [File type matching](../commands/f.md) | n/a | `f flags` <br/><br/> eg `f +s` (only return symlinks) | 77 | Chaining | n/a | eg `f +f \| g *.txt \| !g murex.*` <br/> (returns only files with the extension "txt" that aren't called "murex") | 78 79 ### Expressions 80 | Description | Bash | Murex | 81 |---------------|---------------|--------| 82 | Assignment | `foobar = $((1 + 2 * 3))` | `foobar = 1 + 2 * 3` [[2]](#footnotes) | 83 | Comparison, string | `[ "$(command parameters...)" == "value" ]` | `command(parameters...) == "value"` [[2]](#footnotes) [[7]](#footnotes) <br/><br/> `${command parameters...} == "value"` [[2]](#footnotes) [[5]](#footnotes) | 84 | Comparison, numeric | `[ $integer -eq 5 ]` | `$number == 5` [[2]](#footnotes) | 85 | Arithmetic | `echo $(( 1+2*3 ))` | `1 + 2 * 3` [[2]](#footnotes) <br/><br/> `out (1+2*3)` [[2]](#footnotes) [[5]](#footnotes) | 86 | Supported data types | 1. String,<br/>2. Integer<br/>(all variables are strings) | 1. String,<br/>2. Integer,<br/>3. Float (default number type),<br/>4. Boolean<br/>5. Array,<br/>6. Object,<br/>7. Null<br/>(all variables can be treated as strings and/or their primitive) | 87 88 ### Variables 89 | Description | Bash | Murex | 90 |---------------|---------------|--------| 91 | [Printing a variable](../parser/scalar.md) | `echo "$foobar"` | `out $foobar` [[5]](#footnotes)<br/><br/>`$foobar` <br/><br/> (variables don't need to be quoted in Murex) | 92 | [Assign a local variable](../commands/set.md) | `local foo="bar"` | `$foo = "bar"` [[2]](#footnotes) [[6]](#footnotes)<br/><br/>`out "bar" \| set $foo` | 93 | [Assign a global variable](../commands/global.md) | `foo="bar"` | `$GLOBAL.foo = "bar"` [[6]](#footnotes)<br/><br/>`out "bar" \| global $foo` | 94 | [Assign an environmental variable](../commands/export.md) | `export foo="bar"` | `export foo = "bar"` [[1]](#footnotes) [[2]](#footnotes) [[3]](#footnotes)<br/><br/>`$ENV.foo = "bar"` [[6]](#footnotes)<br/><br/>`out "bar" \| export $foo` [[3]](#footnotes) | 95 | [Assign with a default value](../parser/null-coalescing.md) | `FOOBAR="${VARIABLE:-default}"` | `$foobar = $variable ?? "default"` | 96 97 ### Arrays 98 (eg arrays, lists) 99 | Description | Bash | Murex | 100 |---------------|---------------|--------| 101 | Creating an array | `array_name=(value1 value2 value3)` | `%[value1 value2 value3]` <br/><br/>`%[value1, value2, value3]` <br/><br/> eg `array_name = %[1, 2, 3]`, <br/> eg `%[hello world] \| foreach { ... }`| 102 | Accessing an array element | `${array_name[0]}` | `$array_name[0]` (immutable) <br/><br/>`$array_name.0` (mutable) [[5]](#footnotes) <br/><br/> `array \| [0]` | 103 | Printing multiple elements | `echo ${array_name[1]} ${array_name[0]}` | `@array_name[1 0]` <br/><br/> `array \| [1 0]` | 104 | Printing a range of elements | n/a | `@array_name[1..3]` <br/><br/>`array \| [1..3]` | 105 | [Printing all elements](../parser/array.md) | `echo ${array_name[*]}` | `@array_name` | 106 | [Iterating through an array](../commands/foreach.md) | `for item in array; do;`<br/> `$item`<br/>`done;` | `array \| foreach item { $item }` <br/><br/> eg `%[Tom Richard Sally] \| foreach name { out "Hello $name" }` | 107 108 ### Objects 109 (eg JSON objects, maps, hashes, dictionaries) 110 | Description | Bash | Murex | 111 |---------------|---------------|--------| 112 | Creating an object | n/a | `%{ key: value, array: [1, 2, 3] }` [[2]](#footnotes) <br/><br/> eg `object_name = %{ key: val, arr: [1,3,3] }` <br/> eg `%{ a:1, b:2, c:3 } \| formap { ... }` | 113 | Accessing an element | n/a | `$object_name[key]` (immutable) <br/><br/> `$object_name.key` [[5]](#footnotes) (mutable) <br/><br/> `object \| [key]` | 114 | Printing multiple elements | n/a | `$object_name[key1 key2]` <br/><br/> `object \| [key1 key2]` | 115 | Accessing a nested element | n/a | `$object_name[[.path.to.element]]` (immutable) [[4]](#footnotes)<br/><br/> `$object_name.path.to.element` (mutable)<br/><br/> `object \| [[.path.to.element]]` [[4]](#footnotes)<br/><br/> 116 | [Iterating through an map](../commands/formap.md) | n/a | `object \| formap key value { $key; $value }` <br/><br/> eg `%{Bob: {age: 10}, Richard: {age: 20}, Sally: {age: 30} } \| formap name person { out "$name is $person[age] years old" }` | 117 118 ### Sub-shells 119 | Description | Bash | Murex | 120 |---------------|---------------|--------| 121 | Sub-shell, string | `"$(commands)"` <br/><br/> eg `"echo $(echo "Hello world")"` | `${commands}` [[5]](#footnotes) <br/><br/> eg `out ${out Hello world}` | 122 | Sub-shell, arrays | `$(commands)` <br/><br/> eg `$(echo 1 2 3)` | `@{commands}` [[5]](#footnotes) <br/><br/> eg `out @{ %[1,2,3] }` | 123 | In-lined functions | n/a | `function(parameters...)` [[7]](#footnotes) <br/><br/> eg `out uname(-a)` | 124 125 ### Common one-liners 126 | Description | Bash | Murex | 127 |---------------|---------------|--------| 128 | Add `$PATH` entries | `export PATH="$PATH:/usr/local/bin:$HOME/bin"` | The same Bash code works in Murex too. However you can also take advantage of Murex treating `$PATH` as an array <br/><br/>`%[ @PATH /usr/local/bin "$HOME/bin" ] \| format paths \| export $PATH` | 129 | Iterate directories | `for i in $(find . -maxdepth 1 -mindepth 1 -type d); do`<br/> `echo $i`<br/>`done`| `f +d \| foreach $dir {`<br/> `out $i`<br/>`}` | 130 | If `$dir` exists... | `if [ -d "$dir" ]; then`<br/> `# exists`<br/>`fi` | `if { g $dir \| f +d } then {`<br/> `# exists`<br/>`}` | 131 | Print current directory | `result=${PWD##*/}; result=${result:-/}; printf '%s' "${PWD##*/}"` ([read more](https://stackoverflow.com/a/1371283)) | `$PWD[-1]` | 132 ### Footnotes 133 134 1. Supported for compatibility with traditional shells like Bash. 135 2. Unlike Bash, whitespace (or the absence of) is optional. 136 3. Environmental variables can only be stored as a string. This is a limitation of current operating systems. 137 4. Path separator can be any 1 byte wide character, eg `/`. The path separator is defined by the first character in a path. 138 5. 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 command `foobar`, while `$(foobar)` makes more sense as the variable `$foobar`. 139 6. When assigning a variable where the right hand side is an expression, eg `$foo = "bar"`, the dollar prefix is optional. The `set`, `global` and `export` keywords are considered deprecated. 140 7. The `command(parameters...)` only works for commands who's names match the following regexp pattern: `[._a-zA-Z0-9]+`. Which is exclusively uppercase and lowercase English letters, numbers, fullstop / period, and underscore. 141 142 ## See Also 143 144 * [Array (`@`) Token](../parser/array.md): 145 Expand values as an array 146 * [Named Pipes](../user-guide/namedpipes.md): 147 A detailed breakdown of named pipes in Murex 148 * [Terminal Hotkeys](../user-guide/terminal-keys.md): 149 A list of all the terminal hotkeys and their uses 150 * [`&&` And Logical Operator](../parser/logical-and.md): 151 Continues next operation if previous operation passes 152 * [`>>` Append File](../parser/greater-than-greater-than.md): 153 Writes STDIN to disk - appending contents if file already exists 154 * [`>>` Append Pipe](../parser/pipe-append.md): 155 Redirects STDOUT to a file and append its contents 156 * [`[ ..Range ]`](../parser/range.md): 157 Outputs a ranged subset of data from STDIN 158 * [`[[ Element ]]`](../parser/element.md): 159 Outputs an element from a nested structure 160 * [`runmode`](../commands/runmode.md): 161 Alter the scheduler's behaviour at higher scoping level 162 * [`string` (stringing)](../types/str.md): 163 string (primitive) 164 * [`try`](../commands/try.md): 165 Handles non-zero exits inside a block of code 166 * [`trypipe`](../commands/trypipe.md): 167 Checks for non-zero exits of each function in a pipeline 168 * [`|>` Truncate File](../parser/greater-than.md): 169 Writes STDIN to disk - overwriting contents if file already exists 170 * [`||` Or Logical Operator](../parser/logical-or.md): 171 Continues next operation only if previous operation fails 172 * [index](../parser/item-index.md): 173 Outputs an element from an array, map or table 174 175 <hr/> 176 177 This document was generated from [gen/user-guide/rosetta-stone_doc.yaml](https://github.com/lmorg/murex/blob/master/gen/user-guide/rosetta-stone_doc.yaml).