In Bash, you can define variables by simply assigning a value to a variable name without any spaces around the equal sign (=). Here’s the syntax:
variable_name=value
Example:
- Defining a String Variable:
greeting="Hello, World!"
- Defining an Integer Variable:
count=10
- Defining a Variable with Spaces:
If you want to include spaces in the value, you should enclose the value in quotes:
message="This is a message with spaces."
Accessing Variables:
To access the value of a variable, use the dollar sign ($) before the variable name:
echo $greeting
This will output:
Hello, World!
Note:
- Variable names should not start with a number and can contain letters, numbers, and underscores.
- By convention, variable names are usually written in uppercase letters.
