mshell has two variable namespaces: mshell variables (typed values stored in the interpreter) and environment variables (strings stored in the process environment). They use different sigils and behave differently.

mshell Variables § Back to top

Store a value with the ! suffix and retrieve it with @. Storing pops the top of the stack; retrieval pushes the stored value back onto the stack.

42 answer!
@answer 1 + total!
@total wl

You can store multiple values at once by separating the store tokens with commas. A trailing comma after the last store is ignored.

# Storing multiple values at once
1 2 3 a!, b!, c! # a is 1, b is 2, c is 3.
@a @b @c

# A trailing comma after the last store is ignored
4 5 a!, b!,

Storing values at once is useful in definitions, so that the variables appear in order of the stack, instead of revserse.

def myDef (int str path -- )
    myInt!, myStr!, myPath!
    # Do things..
end

Retrieving an unknown variable results in an error. mshell variables do not consult the environment, so @name only looks in the mshell variable map.

Environment Variables § Back to top

Environment variables are accessed with $NAME. They are always strings. Use $NAME! to set them and $NAME? to check for existence. Reading a missing environment variable with $NAME is an error. Environment variables are always exported to subprocesses, so setting them affects subsequent command executions.

"info" $LOG_LEVEL!
$LOG_LEVEL wl

$EDITOR? if
    $EDITOR wl
else
    "vim" wl
end

You can print all environment variables (sorted by key) using the built-in env function.

.env
"/tmp" $TMPDIR!
['my-tool']!

PWD and OLDPWD § Back to top

Changing directories updates PWD and OLDPWD in the environment. You can read them the same way as any other environment variable.

$PWD wl
`/tmp` cd
$OLDPWD wl