[Short Tip] Access system variables in Nushell

Working with variables in Nushell works mostly like you would expect it. If you want to define a variable, you need the keyword let:

❯ let my_variable = "hello blog readers"

❯ echo $my_variable
hello blog readers

Note that you need the spaces around the equal sign character!

❯ let my_variable="hello blog readers"
error: let requires the equals sign parameter
   ┌─ shell:21:1
   │
21 │ let my_variable="hello blog readers"
   │ ^^^ requires the equals sign parameter

But apart from defining your own variables, there is a surprised when you try to access typical system variables:

❯ echo $HOME
error: Variable not in scope
   ┌─ shell:25:6
   │
25 │ echo $HOME
   │      ^^^^^ unknown variable: $HOME

The reason for that error is that Nushell places system environment variables underneath the main variable $nu:

❯ echo $nu|pivot
───┬─────────────────┬────────────────────────────────────────────
 # │     Column0     │                  Column1                   
───┼─────────────────┼────────────────────────────────────────────
 0 │ env             │ [row 34 columns]                           
 1 │ history-path    │ /home/liquidat/.local/share/nu/history.txt 
 2 │ config          │ [row path prompt startup]                  
 3 │ config-path     │ /home/liquidat/.config/nu/config.toml      
 4 │ path            │ [table 5 rows]                             
 5 │ cwd             │ /home/liquidat
 6 │ home-dir        │ /home/liquidat
 7 │ temp-dir        │ /tmp                                       
 8 │ keybinding-path │ /home/liquidat/.config/nu/keybindings.yml  
───┴─────────────────┴────────────────────────────────────────────

❯ echo $nu.env|pivot
────┬──────────────────────────┬────────────────────────────────────────────────────
 #  │         Column0          │                      Column1                       
────┼──────────────────────────┼────────────────────────────────────────────────────
  0 │ CMD_DURATION_MS          │ 2                                                  
  1 │ HOME                     │ /home/liquidat                                            
  2 │ SYSTEMD_EXEC_PID         │ 2958                                               
  3 │ SSH_AUTH_SOCK            │ /run/user/1000/keyring/ssh                         
  4 │ SESSION_MANAGER          │ local/unix:@/tmp/.ICE-unix/2557,unix/unix:/tmp/.IC 
    │                          │ E-unix/2557                                        
  5 │ GNOME_TERMINAL_SCREEN    │ /org/gnome/Terminal/screen/71e37281_4af2_4c9b_be19 
[...]

❯ echo $nu.env.HOME
/home/liquidat

The environment variables are generated from the environment Nushell was started in. If you need to update those for the current environment, use the command let-env:

❯ echo $nu.env.LANG
en_US.UTF-8

❯ let-env LANG = C

❯ echo $nu.env.LANG
C

Image by Susanne Westphal from Pixabay

One thought on “[Short Tip] Access system variables in Nushell”

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.