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
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-zandA-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"
2. Create an integer variable, such as age, and set it to 20:
$ age=20
3. Create a new variable_data2 string variable, using a digit in its name:
$ variable_data2="Hello World"
4. Create a new combine.sh Bash script file using a text editor like nano:
$ nano combine.sh
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"
Save and close the file.
6. Run the script:
$ bash combine.sh
Output:
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:
$ app_name="apache"
2. Retrieve the app_name value using echo:
$ echo $app_name
Output:
apache
3. Create a digit1 integer variable and set it to 1:
$ digit1=1
4. Create another digit2 integer variable and set it to 2:
$ digit2=2
5. Create a sum variable that adds the digit1 and digit2 variables:
$ sum=$((digit1 + digit2))
6. Retrieve the sum value to view the result of the arithmetic operation:
$ echo "The total is: $sum"
Output:
The total is: 3
7. Create a new script, such as script.sh, to combine the variables and output their values:
$ nano script.sh
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"
Save and close the file.
9. Run the script using Bash:
$ bash script.sh
Output:
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:
$ my_array=("value1" "value2" "value3" "value4")
2. Retrieve the first value by referencing index 0:
$ echo ${my_array[0]}
Output:
value1
3. Retrieve the fourth value:
$ echo ${my_array[3]}
Output:
value4
4. Create a new arrays.sh Bash script to combine the variables in a single file:
$ nano arrays.sh
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]}
Save and close the file.
6. Run the script using Bash:
$ bash arrays.sh
Output:
value1
value4
5. Use Variables to Store and Manipulate Data
1. Create a new variables.sh Bash script:
$ nano variables.sh
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"
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
Output:
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.
Top comments (0)