How to use variables in a BASH shell script

- by

Here’s how to use simple variables in BASH shell scripts. It appears there are no data types, and everything’s a string (correct me if I’m wrong). We can define a variable by first setting it to a value, then later refer to that value with a dollar sign in front of the variable name.

Here’s an example:

#!/bin/bash

VARIABLE="Testing"
echo $VARIABLE

Note that there are no spaces between the variable name, the equal sign or the value. Adding those will result in a runtime error.

Variables can be defined in upper or lower case letters, or a combination thereof.

BASH Variables have a global scope, unless they are prefaced with the local keyword inside functions (in which case, only said function will have access to its value).



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

Leave a Comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.