---
title: How to Use Variables in Bash
url: https://docs.vultr.com/how-to-use-variables-in-bash
description: Learn how to effectively use variables in Bash scripting, including declaration, assignment, scope, and best practices for shell script development.
publish_date: 2024-10-04T11:36:40.516505Z
last_updated: 2024-10-04T11:36:40.516505Z
author: Gilbert Nkolo
header_image: https://docs.vultr.com/public/doc-assets/1949/d0516f41-200c-4198-9674-dcbcd04896e9.png
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

## Introduction

Variables in Bash are symbolic names that store data references in a script. You can use Bash variables in conditions, loops, and user input. They handle different data types, including arithmetic, symbolic characters, and string values.

This article explains how to use variables in Bash scripting to store and manipulate data.

## Variables Syntax in Bash

Variables in Bash use a direct naming syntax followed by an equals sign `=` to store a value. A variable name should not contain a space and an equals sign. The following is a basic variable syntax in Bash:

```
variable_name=value
```

## Create Variables in Bash

Variables can store different types of data, including user input. To create a variable, specify a name and assign it a value. You can manipulate variables in different contexts like conditional structures or loops, to store different values when executing specific commands.

Strings and special character values use double quotes `""` to instruct Bash not to interpret the value as a command while arithmetic values do not require quotes `""` to store data. Variable names also support the following characters:

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

Follow the steps below to create and assign variable in a Bash shell environment or script.

1. Create a new string variable such as `name` and set the value to `JohnDoe`.

    ```console
    $ name="JohnDoe"
    ```
    You can set a variable in Bash to store strings or numbers and later retrieve them for use in scripts or commands. Bash variables in strings allow you to embed or manipulate text dynamically within your scripts.

1. Create an integer variable such as `age` and set the value to `20`.

    ```console
    $ age=20
    ```

1. Create a new `variable_data2` string variable and use a numeric character `2` in its name.

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

1. Create a new `combine.sh` Bash script file using a text editor like Nano to combine the variables.

    ```console
    $ nano combine.sh
    ```

1. 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.

1. Run the script.

    ```console
    $ bash combine.sh
    ```

   Output:

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

## Retrieve Variable Value

Append the `$` character before a variable name to retrieve the variable's value. When you call a variable, Bash retrieves the most recent value. Follow the steps below to retrieve variable data.

1. Create a new `app_name` variable and set the value to `apache`.

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

1. Retrieve the `app_name` variable value using the `echo` command.

    ```console
    $ echo $app_name
    ```

    Output:

    ```
    apache
    ```

1. Create a `digit1` integer variable and set the value to `1`.

    ```console
    $ digit1=1
    ```

1. Create another `digit2` integer variable and set the value to `2`.

    ```console
    $ digit2=2
    ```

1. Create a `sum` variable and set the following function to sum the `digit1` and `digit2` variables.

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

1. Retrieve the `sum` variable value to view the arithmetic operation result.

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

    Output:

    ```
    The total is: 3
    ```

1. Create a new Bash script, such as `script.sh` to combine the variables and output the respective values.

    ```console
    $ nano script.sh
    ```

1. 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.

1. Run the script using Bash.

    ```console
    $ bash script.sh
    ```

    Output:

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

## Use Variables with Arrays in Bash

You can use variables to store multiple values when working with arrays in Bash. You can then retrieve the value of each variable in the array by referencing its index. Variables in arrays help you fetch specific data in operations such as conditional statements. Follow the steps below to use variables with arrays in Bash.

1. Create a new array variable such as `my_array` and set the following multiple string values.

    ```console
    $ my_array=("value1" "value2" "value3" "value4")
    ```
    You can set a variable in bash to store arrays, allowing you to access specific elements using indexed positions.
    
1. Retrieve the first value in the variable by referencing a `0` index.

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

    Output:

    ```
    value1
    ```

1. Retrieve the fourth value in the variable.

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

    Output:

    ```
    value4
    ```

1. Create a new  `arrays.sh` Bash script to combine the variables in a single file.

    ```console
    $ nano arrays.sh
    ```

1. 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.

1. Run the script using Bash.

    ```console
    $ bash arrays.sh
    ```

    Output:

    ```
    value1
    value4
    ```

## Use Variables in Bash Scripts to Store and Manipulate Data

Variables store and allow you to manipulate data in Bash scripts when executing specific commands or conditions. Follow the steps below to create a Bash script that reads the user's input, stores it in a new variable, retrieves the variable data, and manipulates the variable with a new value to output a final result.

1. Create a new `variables.sh` Bash script.

    ```console
    $ nano variables.sh
    ```

1. 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 above script creates and assign new variable `name` from the user's input and outputs the variable value in bash script. The script then manipulates the variable value and converts it to uppercase using `^^` to output the new value.

1. Run the script using Bash.

    ```console
    $ bash variables.sh
    ```

    Output:

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

## Conclusion

You have used variables in Bash to store and manipulate data. Variables can store different data types when working with arithmetic operations, conditional statements, or loops to generate final results based on the variable's data.
