Expressions
Do has three expression contexts: full expressions (inside delimiters), compact
expressions (after $), and string interpolation (inside quoted strings).
Full Expressions
Within parentheses (), brackets [], and braces {}, Do uses C-like syntax
where whitespace is insignificant and operators are available:
Full expressions support:
- Arithmetic operators:
+,-(including unary negation),*,/,//,% - Comparison operators:
==,!=,<,<=,>,>= - Logical operators:
&&,||,! - Bitwise operators:
&,|,^,~,<<,>> - Function calls:
- Juxtaposition:
echo "hello" "world" - C-style:
echo("hello", "world"),func()
- Juxtaposition:
- Indexing:
arr[0],dict["key"] - Field access:
obj.field
Full expressions can span multiple lines:
Operator Precedence
From lowest to highest:
$(low-precedence call)||&&==,!=,<,<=,>,>=|^&<<,>>+,-*,/,//,%- Unary
-,!,~ - Call, index, field access
Ternary-Style Expressions
There is no dedicated ternary operator, but && and || short-circuit and
return their operands (as in Lua or Python), so you can write:
Compact Expressions
The $ prefix introduces a compact expression at statement level. It supports:
- Variable access:
$name - Field access:
$person.name - Indexing:
$arr[0] - C-style calls:
$func(arg1, arg2) - Chaining:
$obj.method(arg).field[0] - Boolean not:
$!flag
Implicit
Some special statement forms expect compact expression without $ to avoid
needing to write it in those cases:
- The right-hand side of a
letor assignment - The condition of an
iforwhile - The iteratee of a
for - The scrutinee of a
bind - After
returnorthrow
In fact, using $ unnecessarily in these contexts is a syntax error.
Quoted Strings
Double-quoted strings support interpolation with $. $ behavior is more
conservative than in compact expressions:
- Simple variable substitution works:
"hello $name" - Anything beyond basic variable access must use
$():"result: $(1 + 2)"
let name = Alice
let age = 30
echo "Hello, $name!"
echo "$name is $age years old"
echo "In 10 years: $(age + 10)"
echo "Type: $(type name)"
Escape sequences in quoted strings:
| Sequence | Meaning |
|---|---|
\n |
Newline |
\t |
Tab |
\\ |
Backslash |
\" |
Double quote |
\$ |
Literal dollar sign |
Binary strings (b"...") additionally support hex byte escapes:
| Sequence | Meaning |
|---|---|
\xNN |
Byte with hex value NN (e.g. \xff) |
\xNN is only valid inside binary strings; using it in a regular string is a
syntax error.
See Here Strings for multi-line string literals that use the same interpolation syntax.
See Binary Strings for details on bin
literals and their methods.