Variables in Bash/Shell Scripts and How To Use Them [Tutorial]

Bash Variables and How to Use Them

This article will show you how to use variables in Bash and Shell scripts in Linux.

Bash (or Linux Shell) scripts are files you write containing commands to automate common tasks and save yourself some time.

Variables are things in scripts that hold a value for later use – a number, or a filename, or anything, really.

Here’s how to define and use variables in Linux Shell scripts. These examples should work in the most popular Linux Shells, Bash, and Zsh.

Declaring Bash Variables

Bash VARIABLES ARE UNTYPED by default. You can’t tell it what can be done with a variable – it’s implied.

Essentially, all variables are stored as strings and handled according to context when they are used – if a string contains a number, it will be treated as one if you try to increment with it, etc.

Below, several variables are declared – MY_STRINGMY_FILE_PATH, and MY_NUMBERThese are all untyped variables.

#!/bin/bash          

MY_STRING="Hello Linux!"
MY_FILE_PATH=/path/to/my/file.txt
MY_NUMBER=4

Click here to find out about the #!

‘declare’ Command Syntax

Note that declare is specific to the Bash environment and may not be present if you are using a different shell.

The syntax of declare is as follows:

declare OPTIONS variable_name=variable_value

Note that:

  • OPTIONS is optional and can be picked from the below table to set the type or behavior of the variable
  • variable_name is the name of the variable you wish to define/declare
  • variable_value is the value of said variable
declare option Meaning
-r readonly Declare a read-only variable
-i integer Declare an integer variable
-a array Declare a variable which is an array
-f function(s) Declare a variable which is a function
-x export Declare a variable which can be exported outside of the script

‘declare’ Command Example

Here a string variable is declared (i.e., no OPTIONS supplied), followed by an integer variable.

#!/bin/bash   

declare MY_STRING="Hello Linux!"
declare -i MY_NUMBER=4

Now, if you tried to assign the value of the variable MY_STRING to the value of MY_NUMBER, MY NUMBER would be set to 0 – as the string does not evaluate to a number.

declare offers only basic typing functionality, so it’s not really something to be relied on- but it can be useful for making sure that a variable can only hold a certain type of value.

Using Bash Variables

To use a variable, prefix it with $ (dollar sign). For example:

#!/bin/bash          

MY_STRING="Hello Linux!"
MY_FILE_PATH=/path/to/my/file.txt
MY_NUMBER=4

echo $MY_STRING # Prints the string variable
touch $MY_FILE_PATH # Create or update the file at the given path
MY_NUMBER=$(($MY_NUMBER+1)) # Increment MY_NUMBER by evaluating a mathmatical formula

Note the use of the touch command to create or update the file in the script above.

Note the use of the double brackets (()) – this tells Bash to evaluate the statement contained within them – otherwise, MY_NUMBER would be given a string value containing the characters “$MY NUMBER+1”.

Using Variables in Strings

Sometimes you may need to use a variable’s value in a string – for example, if you’ve collected file paths from user input.

To include a variable in a string, simply use it in the wrapped quotes of another string:

echo "This is my string: $MY_STRING"

Local Variables

Local variables are only available within the scope in which they are created, i.e., inside the particular function or loop where they are defined.

#!/bin/bash

function myFunction {
        local MY_STRING="This string is only available here, in this function"
        echo $MY_STRING
}

echo $MY_STRING # Will print nothing, as the variable was defined locally within a function

 

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

Your email address will not be published. Required fields are marked *