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

Bash Scripting Best Practices Every Beginner Should Know

Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…

1 day ago

How To Create A Self-Signed SSL Certificate Using Bash And OpenSSL

Introduction A self-signed SSL certificate is a certificate that is created and signed by the…

1 day ago

How To Debug Bash Scripts Using bash -x And set Commands

Introduction Debugging is an important part of Bash scripting. When a script does not work…

1 day ago

How To Use Cron Jobs With Bash Scripts For Automation

Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…

1 day ago

How To Use Pipes In Bash Scripts For Command Chaining

Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…

1 day ago

How To Use grep, awk, And sed In Bash Scripts

Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…

1 day ago