Unusual Ways to Use Variables Inside Bash Scripts

You may have used variables in Bash before, but probably not. You are probably aware of variables in Bash shell scripts.

Like other programming and scripting languages, you use variables to store data and then refer to them in future commands.

name="Linux AndreyEx"
echo "Hello $name!"

And it will print “Hello Linux AndreyEx!”

Besides the example above, there are also a surprisingly large number of things you can do with variables, such as populating a default value when a variable is not set and assigning multiple values ​​to a single variable.

Let us show you some unusual and advanced use of variables in scripting. bash…

Using arrays

Except for the above example, the most common type of variables you’ll find in Bash are arrays:

name=("Linux AndreyEx" "It's FOSS")

The above example assigns both Linux AndreyEx and It’s FOSS as the values ​​for the name variable.

But how do you approach accessing each value?

If you run echo $ name, you can see it prints the first value, Linux AndreyEx. To access other values, you need to use a different syntax for variables.

To start fetching other values, let’s start with the following:

echo "Hello ${name}!"

This is the basis for what will allow you to get all the values ​​of the variables, as well as what will allow you to do the rest of the methods for interacting with variables throughout the rest of this article.

Going back to the above example, you can use the syntax $ {variable[number]} to retrieve a specific element from a variable:

echo "Hello ${name[1]}!"

This will print “Hello It’s FOSS!”

If you haven’t seen from the example above, the first element in the array actually starts at 0, not 1.

So, to pull the Linux AndreyEx value, you have to pull the element at position 0:

echo "Hello ${name[0]}!"

Likewise, simply dropping the part [number] just makes it the first element in the array by default (which has index 0).

The index simply means the position of the element in the array. Likewise, index 0 is element 0 within the array.

And they make up all the types of variables you actually find inside Bash.

Let’s now move on to how you can actually work and change the output of these variables when we call them.

Setting a value when a variable is not set

You can revert a default variable to a specific line if the variable is not set like this:


Let’s take this example:

echo "Hello ${name:-nobody}!"

Since the variable name is not set, it will be set by default, nobody, and the above command will print “Hello nobody!”.

If a variable is set beforehand, its value will be used:

name="Linux AndreyEx"
echo "Hello ${name:-nobody}!"

The above command outputs “Hello Linux AndreyEx!”

Setting a value when a variable is already set

You can also set a variable to return a specific value when the variable is set. This will cause the variable to print a specific line whenever it is set, and not print anything when it is not.

The syntax for this method is similar to the previous one, but instead of +, the sign – is used.


Take this example where no variable name is specified. Thus, it will not print anything for that variable part, and the command output will be “Hello!”

echo "Hello ${name:+person}!"

Modify the above example and set the variable name. With this special method, it will not use the already set value, but will use the one you specified.

name="Linux AndreyEx"
echo "Hello ${name:+person}!"

The above command will print “Hello person!”.

You will probably find more uses for the $ {variable: -string} syntax, as some of the benefits of $ {variable: + string} can already be achieved with other tools such as the operator [] testing ():

Indirect variable references

The following example will use the following syntax:


This option is a little more complicated in how it works, but it becomes clear after you see how it works:

name="Linux AndreyEx"

echo "Hello ${!variable}!"

It will print “Hello Linux AndreyEx”.

Symbol! before the variable name causes a substitute for the normal variable name, but then uses the name of that string to find the corresponding variable.

Same way:

  • variable is replaced with the string name, followed by:
  • name is replaced with its Linux AndreyEx value.

Finding the length of a variable

Let’s use the previous method, but replace the symbol! on #:


Using this method will print the length of the variable, but it will be slightly different depending on whether the variable is a string or an array.

Using this syntax on a string will result in variable substitution for the number of characters in the string.

echo "Length is ${#variable}."

It will give 4. But, on the other hand, when you use it in an array, the number of elements inside the array will be printed, that is, the size of the array, which is 2 here.

variable=("name" "word")
echo "Length is ${#variable}."

Upper and lower case strings

And finally, you can capitalize on the lowercase letters of the string using the ^ and operators, respectively.

You put them at the end of the variable name like this:

# Заглавные буквы

# Нижниq регистр

What’s the difference between one and two characters?

Specifying only one $ {variable ^} will change only the first letter, while specifying two ($ {variable ^^}) will change the entire string.

The example below prints Name instead of name:

echo "${variable^}"

But this one prints NAME:

echo "${variable^^}"

Similarly, you can print variables in lowercase.

The example below will print wIDEname:

echo "${variable,}"

And this one will print widename:

echo "${variable,,}"


These are just a few of the ways in which variables are used within Bash. You can find a few more ways to interact with variables in the Bash documentation on variable expansion.

Have questions about how the above examples worked, or is something just not working? Feel free to leave any of these in the comment section below.

Related Posts