# variables - `env` shows environment variables - `set` shows shell-local variables - `printenv` shows local variables for session - `test1="hello world"` sets test1 variable - `echo $test1` references `$test1` variable and outputs `hello world` - by default, an assigned variable does not get inherited by a child process in current shell, instead use `export` - `export test1="hello world"` sets test1 variable and allows inheritance by child processes - `unset test1` removes `test1` variable - `source` adds functions found in the file argument to the current shell - `source` is frequently used for software installs to ensure that the environment is set up properly prior to execution of the install scripts - Unlike executing a script with `./script.sh` or `bash script.sh`, which launches a new subshell, `source` executes the commands within the current shell - `source file_name [arguments]` - `. file_name [arguments]` - the period . is a shorthand alias for `source` ## special variables ![[images/Pasted image 20250802201349.png]] # command substitution - `date=$(date)` assigns output of `date` command to date variable - backticks around command - \``date`\` - place command within `$()` - `$(date)` # math - place equation within \$[equation] such as `$[25*5]` - only works for integers - use `bc` for floating point calculations # conditionals - most common conditionals: `if`, `elif`, `case` - `if` conditional ```bash #!/bin/bash if [ condition ]; then # commands to execute if condition true else # commands to execute if condition untrue fi ``` - `if` + `elif` conditional ```bash #!/bin/bash if [ condition_1 ]; then # commands to execute if condition_1 true elif [ condition_2 ]; then # commands to execute if condition_2 true else # commands to execute if above conditions untrue fi ``` - `case` conditional - cleaner multi-part conditional alternative to `elif` - easier to read if there are many segments with different outputs - `;;` ends each segment - `esac` ends conditional statement ```bash #!/bin/bash # expression = variable or value being evaulated agaisnt each pattern case expression in pattern_1) commands;; pattern_2) commands;; # catchall *) commands;; esac ``` # Loops - common loops: `while`, `for`, `until` - repeat commands for a set number of times or until a condition occurs - be vary of infinite loops - `while` loop - repeats until condition is false - `x=$(($x+1))` increments x - ((x++)) also works ```bash #!/bin/bash while [ CONDITION ]; do # Commands done ``` - `until` loop - repeats until condition is true, otherwise same as `while` - `x=$(($x+1))` increments x - ((x++)) also works ```bash #!/bin/bash until [ CONDITION ]; do # Commands done ``` - `for` loop - iterates through a list of items - could be a range of numbers, a deterministic set of numbers, files within a directory, lines in a file, etc. - `x=$(($x+1))` increments x - ((x++)) also works ```bash #!/bin/bash for <variable> in <list>; do <commands> done ``` ## file tests for loops ![[images/Pasted image 20250804194347.png]] ## integer tests for loops ![[images/Pasted image 20250804194327.png]]