Bash variables are used to store data that can be referenced and manipulated within a Bash script or command line session. They can hold different types of data, such as strings, numbers, or arrays.
Key Points:
- Declaration: You create a variable by assigning a value to it, e.g.,
my_var="Hello". - Accessing Values: To access the value of a variable, prefix it with a dollar sign (
$), e.g.,echo $my_var. - Scope: Variables are typically local to the script or session unless declared as global.
- Types: Common variable types include:
- String: Text data (e.g.,
name="Alice"). - Integer: Numeric data (e.g.,
count=10). - Array: A collection of values (e.g.,
my_array=(1 2 3)).
- String: Text data (e.g.,
Example:
# Declare a variable
greeting="Hello, World!"
# Access and print the variable
echo $greeting
This will output:
Hello, World!
If you have more questions about Bash variables or need examples, let me know!
