How To

Bash String Concatenation: How To Combine Variables And Strings

Introduction

String concatenation in Bash means combining two or more strings together. A string can be normal text, a variable value, command output, file name, directory path, or user input. If you are learning Bash scripting, understanding string concatenation is important because it helps you create dynamic messages, file paths, log names, backup names, and command outputs.

In Bash scripting, string concatenation is simple. You do not need a special operator like +. You can combine strings by placing variables and text next to each other.

What Is String Concatenation In Bash?

String concatenation means joining strings together.

Example:

#!/bin/bashfirst_name="Kali"last_name="Linux"full_name="$first_name $last_name"echo "$full_name"

Output:

Kali Linux

Here, the variables first_name and last_name are combined into one variable called full_name.

Basic Bash String Concatenation

Create a new Bash script:

nano string-concat.sh

Add the following code:

#!/bin/bashsite="KaliLinuxTutorials"domain=".com"website="$site$domain"echo "Website: $website"

Save and run the script:

chmod +x string-concat.sh./string-concat.sh

Output:

Website: KaliLinuxTutorials.com

Notice that $site$domain joins both variable values without any extra space.

Concatenate Strings With Spaces

To add space between strings, include the space inside quotes.

Example:

#!/bin/bashword1="Bash"word2="Scripting"result="$word1 $word2"echo "$result"

Output:

Bash Scripting

This method is useful when creating readable messages in scripts.

Concatenate Variables With Text

You can also combine variables with normal text.

Example:

#!/bin/bashusername=$(whoami)hostname=$(hostname)echo "User $username is currently logged in on $hostname"

Output:

User kali is currently logged in on kali-machine

This is useful for system information scripts, log messages, and cybersecurity automation.

Create Dynamic File Names Using Concatenation

String concatenation is commonly used to create dynamic file names.

Example:

#!/bin/bashdate_now=$(date +%F)backup_name="backup-$date_now.tar.gz"echo "Backup file name: $backup_name"

Output:

Backup file name: backup-2026-05-24.tar.gz

This type of script is useful for backups, reports, and log files.

Using Braces For Clear Variable Names

Sometimes you should use braces {} around variable names to avoid confusion.

Example:

#!/bin/bashname="log"file="${name}_file.txt"echo "$file"

Output:

log_file.txt

Using ${name} makes it clear where the variable name ends.

Conclusion

Bash string concatenation is an important basic skill for Bash scripting. You can combine strings, variables, command output, file names, and paths easily by placing them together. You can also use quotes and braces to make your scripts safer and easier to read.

Once you understand string concatenation, you can create better Bash scripts for automation, Linux administration, backup tasks, log management, and cybersecurity workflows.

Cyber Defence

Recent Posts

What Is a Data Protection Platform and Why Your Organization Needs One

A data protection platform is a unified system that helps organizations discover, classify, monitor, and…

10 hours ago

How Swiss Privacy Rules Affect AI Companies Under GDPR

AI companies operating in or around Switzerland face a compliance challenge that most legal teams…

2 days ago

Swiss FADP vs EU GDPR: Key Differences for AI and Data Privacy

Two major data privacy laws now govern how organizations handle personal data across Europe; the…

3 days ago

Kali Linux Commands Cheat Sheet: Complete Quick Reference

This cheat sheet covers the essential Kali Linux commands every pentester and ethical hacker uses…

1 month ago

What I Wish I Knew Before Learning Malware Analysis and Reverse Engineering

When I first started learning malware analysis and reverse engineering, I thought the hardest part…

1 month ago

git fetch vs git pull: How They Work and When to Use Each

Both git fetch and git pull talk to a remote repository, but they do very different things to your…

2 months ago