github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/docs/parser/scalar.md (about)

     1  # `$Variable`
     2  
     3  > Expand values as a scalar
     4  
     5  ## Description
     6  
     7  The scalar token is used to tell Murex to expand variables and subshells as a
     8  string (ie one single parameter) irrespective of the data that is stored in the
     9  string. One handy common use case is file names where traditional POSIX shells
    10  would treat spaces as a new file, whereas Murex treats spaces as a printable
    11  character unless explicitly told to do otherwise.
    12  
    13  The string token must be followed with one of the following characters: 
    14  alpha, numeric, underscore (`_`) or a full stop / period (`.`).
    15  
    16  
    17  
    18  ## Examples
    19  
    20  **ASCII variable names:**
    21  
    22  ```
    23  » $example = "foobar"
    24  » out $example
    25  foobar
    26  ```
    27  
    28  **Unicode variable names:**
    29  
    30  Variable names can be non-ASCII however they have to be surrounded by
    31  parenthesis. eg
    32  
    33  ```
    34  » $(比如) = "举手之劳就可以使办公室更加环保,比如,使用再生纸。"
    35  » out $(比如)
    36  举手之劳就可以使办公室更加环保,比如,使用再生纸。
    37  ```
    38  
    39  **Infixing inside text:**
    40  
    41  Sometimes you need to denote the end of a variable and have text follow on.
    42  
    43  ```
    44  » $partial_word = "orl"
    45  » out "Hello w$(partial_word)d!"
    46  Hello world!
    47  ```
    48  
    49  **Variables are tokens:**
    50  
    51  Please note the new line (`\n`) character. This is not split using `$`:
    52  
    53  ```
    54  » $example = "foo\nbar"
    55  ```
    56  
    57  Output as a string:
    58  
    59  ```
    60  » out $example
    61  foo
    62  bar
    63  ```
    64  
    65  Output as an array:
    66  
    67  ```
    68  » out @example
    69  foo bar
    70  ```
    71  
    72  The string and array tokens also works for subshells:
    73  
    74  ```
    75  » out ${ %[Mon..Fri] }
    76  ["Mon","Tue","Wed","Thu","Fri"]
    77  
    78  » out @{ %[Mon..Fri] }
    79  Mon Tue Wed Thu Fri
    80  ```
    81  
    82  > `out` will take an array and output each element, space delimited. Exactly
    83  > the same how `echo` would in Bash.
    84  
    85  **Variable as a command:**
    86  
    87  If a variable is used as a commend then Murex will just print the content of
    88  that variable.
    89  
    90  ```
    91  » $example = "Hello World!"
    92  
    93  » $example
    94  Hello World!
    95  ```
    96  
    97  ## Detail
    98  
    99  Strings and subshells can be expanded inside double quotes, brace quotes as
   100  well as used as barewords. But they cannot be expanded inside single quotes.
   101  
   102  ```
   103  » set example="World!"
   104  
   105  » out Hello $example
   106  Hello World!
   107  
   108  » out 'Hello $example'
   109  Hello $example
   110  
   111  » out "Hello $example"
   112  Hello World!
   113  
   114  » out %(Hello $example)
   115  Hello World!
   116  ```
   117  
   118  However you cannot expand arrays (`@`) inside any form of quotation since
   119  it wouldn't be clear how that value should be expanded relative to the
   120  other values inside the quote. This is why array and object builders (`%[]`
   121  and `%{}` respectively) support array variables but string builders (`%()`)
   122  do not.
   123  
   124  ## See Also
   125  
   126  * [Array (`@`) Token](../parser/array.md):
   127    Expand values as an array
   128  * [Reserved Variables](../user-guide/reserved-vars.md):
   129    Special variables reserved by Murex
   130  * [Tilde (`~`) Token](../parser/tilde.md):
   131    Home directory path variable
   132  * [`"Double Quote"`](../parser/double-quote.md):
   133    Initiates or terminates a string (variables expanded)
   134  * [`%(Brace Quote)`](../parser/brace-quote.md):
   135    Initiates or terminates a string (variables expanded)
   136  * [`'Single Quote'`](../parser/single-quote.md):
   137    Initiates or terminates a string (variables not expanded)
   138  * [`(brace quote)`](../parser/brace-quote-func.md):
   139    Write a string to the STDOUT without new line (deprecated)
   140  * [`ja` (mkarray)](../commands/ja.md):
   141    A sophisticated yet simply way to build a JSON array
   142  * [`let`](../commands/let.md):
   143    Evaluate a mathematical function and assign to variable (deprecated)
   144  * [`out`](../commands/out.md):
   145    Print a string to the STDOUT with a trailing new line character
   146  * [`set`](../commands/set.md):
   147    Define a local variable and set it's value
   148  
   149  <hr/>
   150  
   151  This document was generated from [gen/parser/variables_doc.yaml](https://github.com/lmorg/murex/blob/master/gen/parser/variables_doc.yaml).