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

groupdel Command in Linux: Remove a Group and Audit Files

The groupdel command in Linux removes a group from the system. It deletes the group's entry from /etc/group and /etc/gshadow,…

6 hours ago

wc Command in Linux: Count Lines, Words, Characters, and Bytes

The wc command in Linux counts lines, words, characters, and bytes in files or standard input. It…

6 hours ago

top Command in Linux: Monitor Processes and Resource Usage

The top command in Linux provides a real-time view of running processes and system resource usage. From…

6 hours ago

usermod Command in Linux: Modify User Accounts and Groups

The usermod command in Linux modifies existing user account attributes. You can use it to manage group…

6 hours ago

sort Command in Linux: Sort Text, Numbers, Columns, and More

The sort command in Linux reads lines from files or standard input and writes them to standard…

1 day ago

wall Command in Linux: Broadcast Messages to Logged-In Users

The wall command in Linux sends a message to the terminals of all currently logged-in users. The…

1 day ago