Skip to main content

Bash Variables

To use variables in bash, you must declare them first. To do this, you can use the declare command. The following example shows how to declare a variable named my_variable and assign it the value Hello World!.

declare -x my_variable="Hello World!"

To use a variable, you can use the ${} syntax. The following example shows how to use the variable my_variable in a command.

echo ${my_variable}

You don't always need to use the ${} syntax. You can also use the $() syntax. The following example shows how to use the variable my_variable in a command.

echo $(my_variable)

Here is another example:

SOME_VAR="bla bla"
echo "The value of SOME_VAR is: $SOME_VAR"

In the previous example, the variable SOME_VAR is assigned the value bla bla. Then, the variable SOME_VAR is used in a command.