# Variables and Invocations
There are two types of evaluation expressions in Nushell: variables and invocations. You know that you're looking at an evaluation expression because it begins with a dollar sign ($
). This indicates that when Nushell gets the value in this position, it will need to run an evaluation step to process the expression and then use the resulting value. Both evaluation expression forms support a simple form and a 'path' form for working with more complex data.
# Variables
The simpler of the two evaluation expressions is the variable. During evaluation, a variable is replaced by its value.
If we create a variable, we can print its contents by using $
to refer to it:
> let my-value = 4
> echo $my-value
4
# Variable paths
A variable path works by reaching inside of the contents of a variable, navigating columns inside of it, to reach a final value. Let's say instead of 4
, we had assigned a table value:
> let my-value = [[name]; [testuser]]
We can use a variable path to evaluate the variable $my-value
and get the value from the name
column in a single step:
> echo $my-value.name
testuser
# Invocations
What if instead of a value, you needed to run a command and use its output? For this, you can use an invocation.
Invocations also start with $
and are wrapped with parentheses. The parentheses contain a pipeline that will run to completion, and the resulting value will then be used. For example, $(ls)
would run the ls
command and give back the resulting table and $(git branch --show-current)
runs the external git command and returns a string with the name of the current branch.
Invocations can also be pipelines and not just single commands. If we wanted to get a list of filenames larger than ten kilobytes, we can use an invocation to run a pipelines and assign this to a variable:
> let names-of-big-files = $(ls | where size > 10kb)
> echo $names-of-big-files
───┬────────────┬──────┬──────────┬──────────────
# │ name │ type │ size │ modified
───┼────────────┼──────┼──────────┼──────────────
0 │ Cargo.lock │ File │ 155.3 KB │ 17 hours ago
1 │ README.md │ File │ 15.9 KB │ 17 hours ago
───┴────────────┴──────┴──────────┴──────────────
# Invocation paths
Invocations also support paths. For example, let's say we wanted to get a list of the filenames in the current directory. One way to do this is to use a pipeline:
> ls | get name
We can do a very similar action in a single step using an invocation path:
> echo $(ls).name
It depends on the needs of the code and your particular style which form works best for you.
← Math Environment →