Shell Variables


Shell Linux Essentials/Topic 2/Variables are used to store information and access them later.

Read/write variables

To store data in a variable, just assign it a value using the equal = sign without the dollar sign ($).

variable="Store this value"
Important

Remember to not use any spaces when assigning a value to a variable

To access them, use the dollar sign $ followed by it's identifier:

echo $variable

or curly brackets {}

echo ${variable}

How to use them

Here's an example on how to declare, assign a value and call a variable in a script.

shell_script.sh

#!/bin/bash
var1="Hello"
echo $var1 World
user@host:~$ ./shell_script.sh
Hello World