DEV Community

Cover image for Using Variables in Bash
Sanskriti Harmukh for Vultr

Posted on with Aashish Chaurasiya Originally published at docs.vultr.com

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.


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:

variable_name=value
Enter fullscreen mode Exit fullscreen mode

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:

$ name="JohnDoe"
Enter fullscreen mode Exit fullscreen mode

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

$ age=20
Enter fullscreen mode Exit fullscreen mode

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

$ variable_data2="Hello World"
Enter fullscreen mode Exit fullscreen mode

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

$ nano combine.sh
Enter fullscreen mode Exit fullscreen mode

5. Add the following contents to the combine.sh file:

#!/bin/bash

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

echo "$variable_data2 age: $age name: $name" 
Enter fullscreen mode Exit fullscreen mode

Save and close the file.

6. Run the script:

$ bash combine.sh
Enter fullscreen mode Exit fullscreen mode

Output:

Hello World age: 20 name: JohnDoe
Enter fullscreen mode Exit fullscreen mode

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:

$ app_name="apache"
Enter fullscreen mode Exit fullscreen mode

2. Retrieve the app_name value using echo:

$ echo $app_name
Enter fullscreen mode Exit fullscreen mode

Output:

apache
Enter fullscreen mode Exit fullscreen mode

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

$ digit1=1
Enter fullscreen mode Exit fullscreen mode

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

$ digit2=2
Enter fullscreen mode Exit fullscreen mode

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

$ sum=$((digit1 + digit2))
Enter fullscreen mode Exit fullscreen mode

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

$ echo "The total is: $sum" 
Enter fullscreen mode Exit fullscreen mode

Output:

The total is: 3
Enter fullscreen mode Exit fullscreen mode

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

$ nano script.sh
Enter fullscreen mode Exit fullscreen mode

8. Add the following contents to the file:

#!/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" 
Enter fullscreen mode Exit fullscreen mode

Save and close the file.

9. Run the script using Bash:

$ bash script.sh
Enter fullscreen mode Exit fullscreen mode

Output:

apache
The variable values are: 1, 2
The total is: 3
Enter fullscreen mode Exit fullscreen mode

4. Use Variables with Arrays in Bash

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

$ my_array=("value1" "value2" "value3" "value4")
Enter fullscreen mode Exit fullscreen mode

2. Retrieve the first value by referencing index 0:

$ echo ${my_array[0]} 
Enter fullscreen mode Exit fullscreen mode

Output:

value1
Enter fullscreen mode Exit fullscreen mode

3. Retrieve the fourth value:

$ echo ${my_array[3]} 
Enter fullscreen mode Exit fullscreen mode

Output:

value4
Enter fullscreen mode Exit fullscreen mode

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

$ nano arrays.sh
Enter fullscreen mode Exit fullscreen mode

5. Add the following contents to the arrays.sh file:

#!/bin/bash

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

echo ${my_array[0]} 
echo ${my_array[3]} 
Enter fullscreen mode Exit fullscreen mode

Save and close the file.

6. Run the script using Bash:

$ bash arrays.sh
Enter fullscreen mode Exit fullscreen mode

Output:

value1
value4
Enter fullscreen mode Exit fullscreen mode

5. Use Variables to Store and Manipulate Data

1. Create a new variables.sh Bash script:

$ nano variables.sh
Enter fullscreen mode Exit fullscreen mode

2. Add the following code to the variables.sh file:

#!/bin/bash

read -p "Please enter your name: " name

echo "Hello: $name"

name_uppercase="${name^^}"

echo "Hello again: $name_uppercase"
Enter fullscreen mode Exit fullscreen mode

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:

$ bash variables.sh
Enter fullscreen mode Exit fullscreen mode

Output:

Please enter your name: example
Hello: example
Hello again: EXAMPLE
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)