# Using Variables in Bash

Variables in Bash are symbolic names that store data references in a script, and you can use them in conditions, loops, and with user input to handle different data types, including arithmetic values, symbolic characters, and strings. This guide covers variable naming and assignment rules, how to create and retrieve variable values, how to use variables together with arrays, and how to read user input into a variable and manipulate its value. By the end, you'll have scripts that assign, retrieve, combine, and transform variable data, including reading and uppercasing user input, following Bash scripting practices documented in [**Vultr Docs**](https://docs.vultr.com/how-to-use-variables-in-bash).

---

## 1\. Learn the Variables Syntax

Variables in Bash use a direct naming syntax followed by an equals sign `=` to store a value. A variable name shouldn't contain a space before the `=` sign. The basic variable syntax in Bash is:

```plaintext
variable_name=value
```

Strings and values with special characters should be wrapped in double quotes `""` so Bash doesn't interpret them as a command, while arithmetic values don't require quotes. Variable names also support:

- **Letters:** `a-z` and `A-Z`.
- **Digits:** `0-9` (you can't use a digit as the first character in a variable name).
- **Underscores:** `_`.

## 2\. Create Variables in Bash

**1\. Create a new string variable, such as** `name`**, and set it to** `JohnDoe`**:**

```console
name="JohnDoe"
```

**2\. Create an integer variable, such as** `age`**, and set it to** `20`**:**

```console
age=20
```

**3\. Create a new** `variable_data2` **string variable, using a digit in its name:**

```console
variable_data2="Hello World"
```

**4\. Create a new** `combine.sh` **Bash script file using a text editor like** `nano`**:**

```console
nano combine.sh
```

**5\. Add the following contents to the** `combine.sh` **file:**

```bash
#!/bin/bash

name="JohnDoe"
age=20
variable_data2="Hello World"

echo "$variable_data2 age: $age name: $name" 
```

Save and close the file.

**6\. Run the script:**

```console
bash combine.sh
```

Output:

```plaintext
Hello World age: 20 name: JohnDoe
```

## 3\. Retrieve a Variable's Value

Prepend the `$` character to a variable name to retrieve its value — Bash always returns the most recently assigned value.

**1\. Create a new** `app_name` **variable and set it to** `apache`**:**

```console
app_name="apache"
```

**2\. Retrieve the** `app_name` **value using** `echo`**:**

```console
echo $app_name
```

Output:

```plaintext
apache
```

**3\. Create a** `digit1` **integer variable and set it to** `1`**:**

```console
digit1=1
```

**4\. Create another** `digit2` **integer variable and set it to** `2`**:**

```console
digit2=2
```

**5\. Create a** `sum` **variable that adds the** `digit1` **and** `digit2` **variables:**

```console
sum=$((digit1 + digit2))
```

**6\. Retrieve the** `sum` **value to view the result of the arithmetic operation:**

```console
echo "The total is: $sum" 
```

Output:

```plaintext
The total is: 3
```

**7\. Create a new script, such as** `script.sh`**, to combine the variables and output their values:**

```console
nano script.sh
```

**8\. Add the following contents to the file:**

```bash
#!/bin/bash

app_name="apache"
echo $app_name

digit1=1
digit2=2

echo "The variable values are: $digit1, $digit2"

sum=$((digit1 + digit2))
echo "The total is: $sum" 
```

Save and close the file.

**9\. Run the script using Bash:**

```console
bash script.sh
```

Output:

```plaintext
apache
The variable values are: 1, 2
The total is: 3
```

## 4\. Use Variables with Arrays in Bash

**1\. Create a new array variable, such as** `my_array`**, with multiple string values:**

```console
my_array=("value1" "value2" "value3" "value4")
```

**2\. Retrieve the first value by referencing index** `0`**:**

```console
echo ${my_array[0]} 
```

Output:

```plaintext
value1
```

**3\. Retrieve the fourth value:**

```console
echo ${my_array[3]} 
```

Output:

```plaintext
value4
```

**4\. Create a new** `arrays.sh` **Bash script to combine the variables in a single file:**

```console
nano arrays.sh
```

**5\. Add the following contents to the** `arrays.sh` **file:**

```bash
#!/bin/bash

my_array=("value1" "value2" "value3" "value4")

echo ${my_array[0]} 
echo ${my_array[3]} 
```

Save and close the file.

**6\. Run the script using Bash:**

```console
bash arrays.sh
```

Output:

```plaintext
value1
value4
```

## 5\. Use Variables to Store and Manipulate Data

**1\. Create a new** `variables.sh` **Bash script:**

```console
nano variables.sh
```

**2\. Add the following code to the** `variables.sh` **file:**

```bash
#!/bin/bash

read -p "Please enter your name: " name

echo "Hello: $name"

name_uppercase="${name^^}"

echo "Hello again: $name_uppercase"
```

Save and close the file. The script reads the user's input into the `name` variable, prints it, then uses `^^` to convert it to uppercase and prints the new value.

**3\. Run the script using Bash:**

```console
bash variables.sh
```

Output:

```plaintext
Please enter your name: example
Hello: example
Hello again: EXAMPLE
```

## Next Steps

- Combine variables with conditional statements to drive decisions in your scripts.
- Use variables inside loops to track state across iterations.
- Explore parameter expansion (like `^^`) for more string manipulation options.
- Build a script that reads multiple pieces of user input into named variables.

For the full guide with additional tips, visit the original article on [**Vultr Docs**](https://docs.vultr.com/how-to-use-variables-in-bash).
