Skip to content

Variables in Bash

Variables are fundamental in Bash scripting, allowing you to store and manipulate text and numbers. This section will cover how to define, use, and manage variables in Bash.


Defining Variables

To create a variable in Bash, simply assign a value to a name without any spaces around the equals sign.

name="Cle Sucre"

In this example, the variable name is assigned the value "Cle Sucre".


Using Variables

To use the value stored in a variable, prepend the variable name with a dollar sign ($).

echo $name

In this example, the echo command prints the value of the variable name.

Variable Usage Example


Exporting Variables

Use the export command to make variables available to sub-processes.

export PATH=$PATH:/new/path

This command adds /new/path to the PATH environment variable.

Export Variable Example


Unsetting Variables

You can remove a variable using the unset command.

unset name

After running this, the variable name will no longer hold any value.

Unset Variable Example


Environment Variables

Environment variables are variables that are available system-wide and are inherited by all spawned child processes and shells.

echo $HOME

This command prints the home directory of the current user.

Environment Variable Example


Variable Expansion

Bash provides powerful mechanisms for manipulating variable values, such as substring extraction and case modification.

echo ${name:0:4}

This prints the first four characters of the variable name.

Variable Expansion Example


Array Variables

Bash supports indexed and associative arrays for storing multiple values.

Indexed Arrays

colors=('red' 'green' 'blue')
echo ${colors[2]}

This example accesses the second element of the colors array. But remember, array indexing starts at 0 :)

Indexed Array Example

Associative Arrays

declare -A fruits
fruits[apple]='red'
fruits[banana]='yellow'
echo ${fruits[apple]}

This prints the color associated with apple, (don't worry about echo ${fruits[CleSucre]}, it's just a joke :D).

Associative Array Example


Special Variables

Bash uses several special variables, like $$ for the process ID of the current shell and $? for the exit status of the last command.

echo $$

This command prints the process ID of the current shell, in this case, 2593740. Yours should be different, if not, play the lottery because you had 1 chance in 9999999 to get the same number as me :D.

Special Variable Example

echo $?

This command displays the exit status of the last executed command, for exemple 0 means success, and 127 means command not found.

Special Variable Example

Here are some common exit codes:

Exit Code Meaning
0 Success
1 General error
2 Misuse of shell builtins
126 Command invoked cannot be executed
127 Command not found
128 Invalid exit argument
128 + n Fatal error signal "n"
130 Script terminated by Control-C
255 Exit status out of range

Next Step

Now you know how the basics of variables in Bash ! Next, let's move on to Text Processing to learn how to manipulate text in Bash.

Previous | Next