# Environment & Shell-Local Variables - `env` shows environment variables ![[images/Pasted image 20250801182235.png]] - -u or --unset option unsets a variable from the environment - `set` shows shell-local and environment variables ![[images/Pasted image 20250801182249.png]] # Built-in/Special Variables - `echo $` shows PID of current shell - confirm with `ps aux | grep -i bash` - when executed, a script creates a temporary sub-shell/env as a child processes of current shell with a separate PID - ![[images/Pasted image 20250802201349.png]] - `echo $SHELL` shows current shell such as `/bin/bash` - `echo $?` shows exit code of previous command - successful exit code (without errors) = 0 - unsuccessful exit code (with errors) =/= 0 - `echo $RANDOM` shows random number with up to 5 digits - `echo $HOME` shows home directory of current user - `echo $USER` shows name of current user - `echo $PWD` shows present working directory - `echo $EPOCHSECONDS` shows current time in Unix time = seconds since 19700101 - only available in BASH v5+ - `echo $EPOCHREALTIME` shows the above with six decimals points - `echo $HOSTTYPE` shows CPU architecture - `echo $OSTYPE` shows OS - `echo $MACHTYPE` shows CPU arch and OS ![[images/Pasted image 20250808143548.png]] # Setting & Exporting Variables - `test="test var"` sets test variable at command line - does not persistence across reboots or exiting shell - child processes (e.g., a script) cannot inherit this type of variable - `echo $test1` will output `test var` - `export test` exports test variable - `export test="test var"` assigns variable and exports it - exported variables can be inherited by child processes - child processes cannot insert variables into parent processes; only works from parent to child - `export MYDATE=$(date +%s)` - creates MYDATE variable similar to $EPOCHSECONDS - `unset test` un-assigns the test variable - command substitution: `date=$(date)` assigns output of `date` command to date variable - case sensitive when referencing variables - often BASH variables are uppercase but this only a common practice and not a requirement