In the bash shell, you can set the value of a shell variable using the following syntax:
variable_name=value
For example:
message="Hello World"
This sets the value of the message
variable to "Hello World"
.
You can also set a variable to the result of a command:
current_date=$(date)
This sets the value of the current_date
variable to the output of the date
command.
Note that when setting a variable to the output of a command, it’s important to use the correct syntax for command substitution, either the backticks (`
) or the dollar sign and parentheses ($( )
).
Once a variable is set, you can reference its value by prefixing the variable name with a dollar sign ($
). For example:
message="Hello World"
echo $message
This will print the value of the message
variable, which is "Hello World"
.
You can also use the value of a variable in the middle of a string by enclosing the variable name in curly braces ({}
) and prefixing it with a dollar sign:
message="Hello World"
echo "The message is: ${message}"
This will print the string "The message is: Hello World"
.