
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 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:
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:
a-z and A-Z0-9 (You can not use a digit as the first character in a variable name.)_Follow the steps below to create and assign variable in a Bash shell environment or script.
Create a new string variable such as name and set the value to 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.
Create an integer variable such as age and set the value to 20.
Create a new variable_data2 string variable and use a numeric character 2 in its name.
Create a new combine.sh Bash script file using a text editor like Nano to combine the variables.
Add the following contents to the combine.sh file.
Save and close the file.
Run the script.
Output:
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.
Create a new app_name variable and set the value to apache.
Retrieve the app_name variable value using the echo command.
Output:
Create a digit1 integer variable and set the value to 1.
Create another digit2 integer variable and set the value to 2.
Create a sum variable and set the following function to sum the digit1 and digit2 variables.
Retrieve the sum variable value to view the arithmetic operation result.
Output:
Create a new Bash script, such as script.sh to combine the variables and output the respective values.
Add the following contents to the file.
Save and close the file.
Run the script using Bash.
Output:
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.
Create a new array variable such as my_array and set the following multiple string values.
You can set a variable in bash to store arrays, allowing you to access specific elements using indexed positions.
Retrieve the first value in the variable by referencing a 0 index.
Output:
Retrieve the fourth value in the variable.
Output:
Create a new arrays.sh Bash script to combine the variables in a single file.
Add the following contents to the arrays.sh file.
Save and close the file.
Run the script using Bash.
Output:
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.
Create a new variables.sh Bash script.
Add the following code to the variables.sh file.
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.
Run the script using Bash.
Output:
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.
0 Comments
Be the first to comment and share your perspective with the community.