*Wednesday, 24th June 2026 - 13:33 UTC+01:00*
# Variables in Bash Scripting
## Basic Variables in Bash
```bash
user@linux:~$ var1="beans"
user@linux:~$ echo $var1
beans
```
```bash
user@linux:~$ firstName="General"
user@linux:~$ lastName="Kenobi"
user@linux:~$ echo "Hello there! $firstName $lastName"
Hello there! General Kenobi
```
Do not forget the `
notation. It's vital for variables in Bash.
Do not add spaces around the `=`.
Using different quotation marks can mean different things in Bash:
- Single quotes (`'some text'`) - shell interprets what is between literally
- Double quotes (`"some text"`) - shell interprets literally, **except** using `
and backticks.
The last way creates a shell-within-a-shell, which can be useful for calling CLI programs. This is done with backticks.<br>
Backticks are where the shell runs the command and captures STDOUT back into a variable.
```bash
user@linux:~$ now_var='NOW'
user@linux:~$ now_var_singlequote='$now_var'
user@linux:~$ echo $now_var_singlequote
$now_var
user@linux:~$ now_var_doublequote="$now_var"
user@linux:~$ echo $now_var_doublequote
NOW
```
```bash
user@linux:~$ date
Wed Jun 24 13:33:33 BST 2026
user@linux:~$ rightnow_doublequote="The date is `date`."
user@linux:~$ echo $rightnow_doublequote
The date is Wed Jun 24 13:33:33 BST 2026
```
In this latest sell script, the date program was called, output captured, and combined in-line with the `echo` call. A shell within a shell. Backtick notation does have its own equivalent:
```bash
user@linux:~$ rightnow_doublequote="The date is `date`."
user@linux:~$ rightnow_parentheses="The date is $(date)."
user@linux:~$ echo $rightnow_doublequote && echo $rightnow_parentheses
The date is Wed Jun 24 13:33:33 BST 2026
The date is Wed Jun 24 13:33:33 BST 2026
```
Both work the same, though the backticks are the older of the two. More modern applications will use parentheses.
## Numeric Variables in Bash
Numbers aren't natively built into the shell like it is in other programming languages such as R and Python.
In Python or REPL, you could do:
```python
>>> 8000000+10000-1800-65
8008135
```
A useful CLI utility program for numerical values is `expr`:
```bash
user@linux:~$ expr 42+27
69
```
However, `expr` cannot handle decimal places, so `bc` can be used instead.
```bash
user@linux:~$ echo "350.25 + 69.75" | bc
420
```
`scale` is an argument within `bc` that is useful for specifying how many decimal places for the return value.
```bash
user@linux:~$ echo "10 / 3" | bc
3
user@linux:~$ echo "scale=3; 10 / 3" | bc
3.333
```
Numeric variables can also be assigned similar to string variables:
```bash
user@linux:~$ cat_name="Meowimus Decimus Meridius"
user@linux:~$ cat_age=10
user@linux:~$ echo "My cat's name is $cat_name and they are $cat_age years old."
My cat's name is Meowimus Decimus Meridius and they are 10 years old.
```
Double bracket notation is a variant of single bracket variable notation for numeric variables:
```bash
user@linux:~$ expr 5 + 7
12
user@linux:~$ echo $((5 + 7))
12
```
Backticks can be very useful for numeric variables:
```bash
user@linux:~$ num1=329.54
user@linux:~$ num2=91.15
user@linux:~$ echo "The total score is $(echo "$num1 + $num2" | bc)"
The total score is 420.69
user@linux:~$ echo "The average score is $(echo "($num1 + $num2) / 2" | bc)"
The average score is 210.345
```
Something like this can be used, for example, when you need to convert Fahrenheit to Celsius:
```bash
user@linux:~$ cat tempConverter.sh
## Start of File
# Get first ARGV into variable
temp_f=$1
# Subtract 32
temp_f2=$(echo "scale=2; $temp_f - 32" | bc)
# Multiply by 5/9
temp_c=$(echo "scale=2; $temp_f2 * 5 / 9" | bc)
# Print the celsius temp
echo $temp_c
## End of File
user@linux:~$ bash script.sh
43.94
```
Variables can also be used to concatenate:
```bash
# Create three variables from three different files
num_a=$(cat folder/file_A)
num_b=$(cat folder/file_B)
num_c=$(cat folder/file_C)
# Print out the three variables
echo "The three temperatures were $num_a, $num_b, and $num_c"
```
## Arrays in Bash
### Array
There are two types of arrays in Bash:
- An array
- 'Normal' numerical-indexed structure
- Also known as a *list* in Python, or a *vector* in R
- In Python, it is formatted: `list = [1,2,3,4]`
- In R, it is formatted: `vector <- c(1,2,3,4)`
There are two ways that a numerical array can be created in Bash:
```bash
declare -a array_name
```
```bash
array_name=(array contents)
```
Do **not** use commas to separate array elements.<br>
All array elements can be returned using `array[@]`, though Bash requires curly brackets around the array name when accessing these properties:
```bash
user@linux:~$ array_name=(1 7 3 8)
user@linux:~$ echo ${array_name[@]}
1 7 3 8
```
The length of the array can be accessed using `#array[@]`
```bash
user@linux:~$ echo ${#array_name[@]}
4
```
Like many other things, array elements can be manipulated. For example, to return the seventh item in the array:
```bash
user@linux:~$ fibonacci=(1 1 2 3 5 8 13 21 34 55 89)
user@linux:~$ echo ${fibonacci[6]}
13
```
The reason it used `fibonacci[6]` instead of `fibonacci[7]` is because for an array like this, Bash uses zero-indexing. This means arrays start at zero, not one. Python is the same.
Array elements can also be set using the index notation:
```bash
user@lunux:~$ numberArray=(2 27 2372 92 191 8008135 420 21 128 127 1)
user@lunux:~$ numberArray[0]=153
user@lunux:~$ echo ${numberArray[0]}
153
```
If you want to 'slice' out a subset of the array, you would use the notation `array[@]:N:M` to do so. In this case, `N` is the starting index, and `M` is how many elements to return.
```bash
user@linux:~$ anotherArray=(15 20 300 42 23 2 4 33 54 69 66)
user@linux:~$ echo ${anotherArray[@]:3:2}
42 23
```
If you want to append to an array, you would so so using `array+=(elements)`. For example:
```bash
user@linux:~$ anotherArray=(300 42 23 2 4 33 54 69 66)
user@linux:~$ anotherArray+=(10)
user@linux:~$ echo ${anotherArray[@]}
300 42 23 2 4 33 54 69 66 10
```
If you do not add parentheses around what you want to append to an array, it will simply add what you are trying to append, just not to the end of the array. It will add it to the first element in the array instead.
### Associative Array
An associative array is similar to a normal array, but it has key-value pairs instead of numerical indexes.<br>
It is similar to Python's dictionary or R's list.<br>
Associative arrays are only avaialble in Bash 4 onwards, though checking the Bash version is as simple as `bash --version`.
In Python, an associative array would look like:
```python
my_dict = {'city_name': "New York", 'population': 14000000}
```
In R, an associative array would look like:
```r
my_list = list(city_name = c('New York'), population = c(14000000))
```
When creating an associative array, keep in mind that it has a slightly different syntax to a normal array. You can only create associative arrays via the declare syntax (and uppercase `-A`).<br>
You can either declare first, then add elements; or do it all on one line.
- Surround 'keys' in square brackers, then associate a value after the equals sign.
- Multiple elements may be added at once.
An example of an associative array being created:
```bash
user@linux:~$ declare -A city_details # Declare first
user@linux:~$ city_details=([city_name]="New York" [population]=14000000) # Add elements
user@linux:~$ echo ${city_details[city_name]} # Index using key to return a value
New York
```
Alternatively, associative arrays can be created and assigned in a single line:
```bash
declare -A city_details=([city_name]="New York" [population]=14000000)
```
The keys of an associative array can be acccessed with an `!`
```bash
echo ${!city_details[@]} # Return all the keys
```
Which outputs:
```
city_name population
```