How to use variables in a BASH shell script

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).





Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.