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.
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.
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.
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.
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.
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.
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.
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.
Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…
Introduction A self-signed SSL certificate is a certificate that is created and signed by the…
Introduction Debugging is an important part of Bash scripting. When a script does not work…
Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…
Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…
Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…