github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/gen/user-guide/rosetta-stone.inc.md (about) 1 {{ if env "DOCGEN_TARGET=vuepress" }}## Rosetta stone{{ end }} 2 3 Below is a reference table of common Bash code and how it could be written in 4 Murex. 5 6 It is also recommended that you read the language [tour](../tour.md) 7 if you want to learn more about shell scripting in Murex. 8 9 {{ if env "DOCGEN_TARGET=" }}<h2>Table of Contents</h2> 10 11 <div id="toc"> 12 13 - [Output \& error streams](#output--error-streams) 14 - [Quoting strings](#quoting-strings) 15 - [Process management](#process-management) 16 - [Comments](#comments) 17 - [File pattern matching](#file-pattern-matching) 18 - [Expressions](#expressions) 19 - [Variables](#variables) 20 - [Arrays](#arrays) 21 - [Objects](#objects) 22 - [Sub-shells](#sub-shells) 23 - [Common one-liners](#common-one-liners) 24 - [Footnotes](#footnotes) 25 26 </div> 27 28 {{ end }} 29 ### Output & error streams 30 | Description | Bash | Murex | 31 |---------------|---------------|--------| 32 | [Write to STDOUT](../commands/out.md) | `echo "Hello Bash"` | `out "Hello Murex"` <br/><br/>`echo "Hello Murex"` [[1]](#footnotes)| 33 | [Write to STDERR](commands/err.md) | `echo "Hello Bash" >2` | `err "Hello Murex"` | 34 | Write to file (truncate) | `echo "Hello Bash" > hello.txt` | `echo "Hello Murex" \|> hello.txt`| 35 | Write to file (append) | `echo "Hello Bash" >> hello.txt` | `echo "Hello Murex" >> hello.txt`| 36 | [Pipe commands](../parser/pipe-arrow.md) | `echo "Hello Bash \| grep Bash` | `echo "Hello Murex \| grep Murex` <br/><br/> `out "Hello Murex" -> regexp m/Murex/` | 37 | [Redirect errors to STDOUT](../parser/pipe-err.md) | `curl murex.rocks 2>&1 \| less` | `curl <!out> murex.rocks \| less` | 38 | Redirect output to STDERR | `uname -a >&2` | `uname <err> -a` | 39 | Ignore STDERR output | `echo something 2>/dev/null` | `echo <!null> something` | 40 | 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}"` | 41 42 ### Quoting strings 43 | Description | Bash | Murex | 44 |---------------|---------------|--------| 45 | [Infixing](../parser/double-quote.md) | `echo "Hello $SHELL"` | `out "Hello $SHELL"` | 46 | [String literals](../parser/single-quote.md) | `echo 'Hello' $SHELL` | `out 'Hello' $SHELL` | 47 | [Nesting quotes](../parser/brace-quote.md) | `echo 'Hello \'Bob\''` | `out %(Hello 'Bob')` | 48 49 ### Process management 50 | Description | Bash | Murex | 51 |---------------|---------------|--------| 52 | [Exit number](../commands/exitnum.md) | `$?` | `exitnum` | 53 | [Background jobs](../commands/bg.md) | `command &` | `bg { command }` | 54 | [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` | 55 | Happy paths | `command && command` | `command && command` <br/><br/> `try {command; command}` | 56 | Unhappy paths | `command \|\| command` | `command \|\| command` <br/><br/> `try {command}; catch {command}` | 57 | Pipe fail | `set -o pipefail` | `runmode trypipe module` <br/><br/> `runmode trypipe function` <br/><br/> `trypipe { commands }` 58 59 ### Comments 60 | Description | Bash | Murex | 61 |---------------|---------------|--------| 62 | Single line | `# comment` | `# comment` | 63 | Multiple lines | `:<<EOC`<br/>`line 1`<br/>`line 2`<br/>`EOC` | `/#`<br/>`line 1`<br/>`line 2`<br/>`#/` | 64 | Mid-line | n/a | eg `out foo/#comment#/bar` 65 66 ### File pattern matching 67 (also known as "wildcards") 68 | Description | Bash | Murex | 69 |---------------|---------------|--------| 70 | [Globbing](../commands/g.md) | eg `ls *.txt` | eg `ls *.txt` (in the interactive terminal) <br/><br/> `g pattern` <br/><br/> eg `ls @{g *.txt}` | 71 | [Regexp](../commands/rx.md) | n/a | `rx pattern` <br/><br/> eg `ls @{rx '*\\.txt'}` | 72 | [File type matching](../commands/f.md) | n/a | `f flags` <br/><br/> eg `f +s` (only return symlinks) | 73 | Chaining | n/a | eg `f +f \| g *.txt \| !g murex.*` <br/> (returns only files with the extension "txt" that aren't called "murex") | 74 75 ### Expressions 76 | Description | Bash | Murex | 77 |---------------|---------------|--------| 78 | Assignment | `foobar = $((1 + 2 * 3))` | `foobar = 1 + 2 * 3` [[2]](#footnotes) | 79 | Comparison, string | `[ "$(command parameters...)" == "value" ]` | `command(parameters...) == "value"` [[2]](#footnotes) [[7]](#footnotes) <br/><br/> `${command parameters...} == "value"` [[2]](#footnotes) [[5]](#footnotes) | 80 | Comparison, numeric | `[ $integer -eq 5 ]` | `$number == 5` [[2]](#footnotes) | 81 | Arithmetic | `echo $(( 1+2*3 ))` | `1 + 2 * 3` [[2]](#footnotes) <br/><br/> `out (1+2*3)` [[2]](#footnotes) [[5]](#footnotes) | 82 | 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) | 83 84 ### Variables 85 | Description | Bash | Murex | 86 |---------------|---------------|--------| 87 | [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) | 88 | [Assign a local variable](../commands/set.md) | `local foo="bar"` | `$foo = "bar"` [[2]](#footnotes) [[6]](#footnotes)<br/><br/>`out "bar" \| set $foo` | 89 | [Assign a global variable](../commands/global.md) | `foo="bar"` | `$GLOBAL.foo = "bar"` [[6]](#footnotes)<br/><br/>`out "bar" \| global $foo` | 90 | [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) | 91 | [Assign with a default value](../parser/null-coalescing.md) | `FOOBAR="${VARIABLE:-default}"` | `$foobar = $variable ?? "default"` | 92 93 ### Arrays 94 (eg arrays, lists) 95 | Description | Bash | Murex | 96 |---------------|---------------|--------| 97 | 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 { ... }`| 98 | Accessing an array element | `${array_name[0]}` | `$array_name[0]` (immutable) <br/><br/>`$array_name.0` (mutable) [[5]](#footnotes) <br/><br/> `array \| [0]` | 99 | Printing multiple elements | `echo ${array_name[1]} ${array_name[0]}` | `@array_name[1 0]` <br/><br/> `array \| [1 0]` | 100 | Printing a range of elements | n/a | `@array_name[1..3]` <br/><br/>`array \| [1..3]` | 101 | [Printing all elements](../parser/array.md) | `echo ${array_name[*]}` | `@array_name` | 102 | [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" }` | 103 104 ### Objects 105 (eg JSON objects, maps, hashes, dictionaries) 106 | Description | Bash | Murex | 107 |---------------|---------------|--------| 108 | 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 { ... }` | 109 | Accessing an element | n/a | `$object_name[key]` (immutable) <br/><br/> `$object_name.key` [[5]](#footnotes) (mutable) <br/><br/> `object \| [key]` | 110 | Printing multiple elements | n/a | `$object_name[key1 key2]` <br/><br/> `object \| [key1 key2]` | 111 | 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/> 112 | [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" }` | 113 114 ### Sub-shells 115 | Description | Bash | Murex | 116 |---------------|---------------|--------| 117 | Sub-shell, string | `"$(commands)"` <br/><br/> eg `"echo $(echo "Hello world")"` | `${commands}` [[5]](#footnotes) <br/><br/> eg `out ${out Hello world}` | 118 | Sub-shell, arrays | `$(commands)` <br/><br/> eg `$(echo 1 2 3)` | `@{commands}` [[5]](#footnotes) <br/><br/> eg `out @{ %[1,2,3] }` | 119 | In-lined functions | n/a | `function(parameters...)` [[7]](#footnotes) <br/><br/> eg `out uname(-a)` | 120 121 ### Common one-liners 122 | Description | Bash | Murex | 123 |---------------|---------------|--------| 124 | 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` | 125 | 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/>`}` | 126 | If `$dir` exists... | `if [ -d "$dir" ]; then`<br/> `# exists`<br/>`fi` | `if { g $dir \| f +d } then {`<br/> `# exists`<br/>`}` | 127 | Print current directory | `result=${PWD##*/}; result=${result:-/}; printf '%s' "${PWD##*/}"` ([read more](https://stackoverflow.com/a/1371283)) | `$PWD[-1]` | 128 ### Footnotes 129 130 1. Supported for compatibility with traditional shells like Bash. 131 2. Unlike Bash, whitespace (or the absence of) is optional. 132 3. Environmental variables can only be stored as a string. This is a limitation of current operating systems. 133 4. Path separator can be any 1 byte wide character, eg `/`. The path separator is defined by the first character in a path. 134 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`. 135 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. 136 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.