In Nushell, environment variables are managed using the $env
variable, which functions as a record containing key-value pairs. This approach is more structured compared to traditional shells like Bash.
To set an environment variable in Nushell, use the following syntax:
$env.VARIABLE_NAME = 'value'
Example:
$env.AWS_ACCESS_KEY_ID = 'ASIDGWF7T'
Note: Ensure there are spaces around the
=
sign to avoid syntax errors.
Retrieve the value of an environment variable like so:
$env.VARIABLE_NAME
Example:
$env.AWS_ACCESS_KEY_ID
To view all environment variables in a table format, you can use:
$env | table -e
This command pipes the $env
record into the table
command with the -e
flag, which formats the environment variables into a structured table.
Feature | Nushell Syntax | Traditional Shell (e.g., Bash) Syntax |
---|---|---|
Setting Variables | $env.VARIABLE_NAME = 'value' |
export VARIABLE_NAME='value' |
Accessing Variables | $env.VARIABLE_NAME |
$VARIABLE_NAME |
Listing Variables | `$env | table -e` |
Advantage | Description |
---|---|
Consistency | Environment variables are managed through the $env record, promoting a structured syntax. |
Readability | $env provides clear access and separation from regular variables. |
Enhanced Table View | Using `$env |
Nushell encapsulates environment variables within the $env
record, providing a more structured and consistent approach to environment management. This differs from traditional shells, where variables are accessed and managed directly, often without clear separation from other shell variables.