Bash Scripting

How To Use Variables In Bash Scripts

Introduction

Variables are one of the most important basics of Bash scripting. A variable is used to store data such as text, numbers, file names, paths, usernames, or command output. Once a value is stored in a variable, you can reuse it multiple times inside your Bash script.

If you are learning Bash scripting in Linux, understanding variables is necessary. Variables help you write clean, flexible, and reusable scripts. They are commonly used in automation, system administration, cybersecurity scripts, backup scripts, and log analysis tasks.

What Is A Bash Variable?

A Bash variable is a name that stores a value. You can create a variable by writing the variable name, followed by the = symbol, and then the value.

Example:

name="Kali Linux"

There should be no spaces around the = symbol. This is very important in Bash.

Correct:

user="admin"

Wrong:

user = "admin"

How To Print A Variable In Bash

To use or print a variable, add the $ symbol before the variable name.

Example:

#!/bin/bash

name="Kali Linux"
echo "Welcome to $name"

Save the file:

nano variables.sh

Give execute permission:

chmod +x variables.sh

Run the script:

./variables.sh

Output:

Welcome to Kali Linux

Bash Variable Example

Here is a simple Bash script using multiple variables:

#!/bin/bash

username="root"
tool="Nmap"
website="kalilinuxtutorials.com"

echo "Username: $username"
echo "Security Tool: $tool"
echo "Website: $website"

This script stores different values and prints them using the echo command.

Using Command Output In Variables

You can also store the output of a Linux command inside a variable. This is useful for system information scripts.

Example:

#!/bin/bash

current_user=$(whoami)
current_date=$(date)
current_directory=$(pwd)

echo "Current User: $current_user"
echo "Current Date: $current_date"
echo "Current Directory: $current_directory"

In this example, $(command) runs the command and stores the output in a variable.

Using Numbers In Bash Variables

Bash variables can also store numbers.

Example:

#!/bin/bash

a=10
b=5
sum=$((a + b))

echo "The total is: $sum"

Output:

The total is: 15

For arithmetic operations, use $(( )).

Best Practices For Bash Variables

Use clear variable names that describe the value. For example, use backup_dir instead of x. Avoid spaces around the = symbol. Use quotes around variables when working with text or paths.

Example:

backup_dir="/home/user/backups"
echo "Backup directory is: $backup_dir"

Conclusion

Variables are a basic but powerful part of Bash scripting. They allow you to store values, reuse data, capture command output, and perform simple calculations. Once you understand variables, you can start writing more useful Bash scripts for automation, Linux administration, and cybersecurity tasks.

Practice creating variables with text, numbers, file paths, and command outputs. This will help you build a strong foundation in Bash scripting.

0xSnow

0xSnow is a cybersecurity researcher with a focus on both offensive and defensive security. Working with ethical hacking, threat detection, Linux tools, and adversary simulation, 0xSnow explores vulnerabilities, attack chains, and mitigation strategies. Passionate about OSINT, malware analysis, and red/blue team tactics, 0xSnow shares detailed research, technical walkthroughs, and security tool insights to support the infosec community.

Recent Posts

Install Samba on Ubuntu 18.04: Configure Shares and User Access

Samba is a free, open-source implementation of the SMB/CIFS network protocol that lets Linux servers share…

11 hours ago

Set Up an OpenVPN Server on Ubuntu 18.04 with EasyRSA and UFW

Running your own VPN gives you full control over your traffic, privacy, and connection security. It encrypts…

11 hours ago

Install IntelliJ IDEA on Ubuntu 18.04 via Snap: Setup Guide

IntelliJ IDEA is a full-featured IDE for JVM and Android development made by JetBrains. It includes…

12 hours ago

Install Steam on Ubuntu 18.04: Multiverse Setup and First Run

Steam is a cross-platform digital distribution platform by Valve Corporation that gives you access to thousands…

12 hours ago

Install Redmine on Ubuntu 18.04 with MySQL, Passenger, and Nginx

Redmine is one of the most popular open-source project management and issue tracking platforms. It is…

12 hours ago

Install VirtualBox on Ubuntu 18.04 from the Oracle Repository

VirtualBox is a free, open-source, cross-platform virtualization application maintained by Oracle. It lets you run multiple…

1 day ago