diff --git a/book/special_variables.md b/book/special_variables.md index cd8c460baff..d740f249a09 100644 --- a/book/special_variables.md +++ b/book/special_variables.md @@ -47,6 +47,54 @@ The `$nu` constant is a record containing several useful values: `$env` is a special mutable variable containing the current environment variables. As with any process, the initial environment is inherited from the parent process which started `nu`. +::: tip In other shells and languages ... + +::: tabs +@tab Nu + +```nu +# Set an environment variable +$env.VARNAME = "Content" + +# Access an environment variable +$env.VARNAME +# => Content + +# View all environment variables +$env +``` + +@tab Bash/POSIX + +````bash:no-line-numbers +# Set an environment variable +VARNAME="Content" +export VARNAME +# or +export VARNAME="Content" + +# Access an environment variable +echo $VARNAME +# => Content + +# View all environment variables +env + +@tab PowerShell + +```powershell +# Set an environment variable +$env:VARNAME = "Content" + +# Access an environment variable +$env:VARNAME +# => Content +```` + + + +::: + There are also several environment variables that Nushell uses for specific purposes: ### `$env.config` @@ -81,6 +129,40 @@ try { # => 2 ``` +::: tip In other shells and languages ... + +::: tabs + +@tab Nu + +```nu +$env.LAST_EXIT_CODE +``` + +@tab Bash/POSIX + +```bash +$? +``` + +@tab PowerShell + +```powershell +# Code +$LASTEXITCODE + +# Successful - true or false +$? +``` + +@tab Perl + +```perl +$? >> 8 +``` + +::: + ### `env.CMD_DURATION_MS` The amount of time in milliseconds that the previous command took to run. @@ -103,6 +185,24 @@ the file resides. Note that this value is also available as a constant through: path self | path dirname ``` +::: tip In other shells and languages ... + +::: tabs + +@tab Nu + +```nu +$env.FILE_PWD +``` + +@tab PowerShell + +```powershell +$PSCommandPath +``` + +::: + ### `$env.PROCESS_PATH` When _executing a script_, this variable represents the name and relative path of the script. Unlike the two variables @@ -110,6 +210,23 @@ above, it is not present when sourcing a file or importing a module. Note: Also unlike the two variables above, the exact path (including symlinks) that was used to _invoke_ the file is returned. +::: tip In other shells and languages ... + +::: tabs +@tab Nushell + +```nu +print $env.PROCESS_PATH +``` + +@tab Bash + +```bash +echo $0 +``` + +::: + ### `$env.NU_LIB_DIRS` A list of directories which will be searched when using the `source`, `use`, or `overlay use` commands. See also: