Bash Scripting

Bash String Concatenation – Bash Scripting


Introduction

If you’re new to Bash scripting, one of the first skills you’ll need is string concatenation joining two or more strings together. Don’t worry if you’re completely new to Bash! If you haven’t started with Bash scripts yet, check out our beginner-friendly guide on how to create and run shell scripts in Bash.

In this guide, you’ll learn six simple ways to combine strings in Bash using easy-to-understand examples. By the end, you’ll be ready to use these methods in your own Linux scripts, whether you’re building simple scripts or more complex programs.

1. Placing One String Next to Another

The simplest way to concatenate strings in Bash is to just place them next to each other:

str1="Hello"
str2="World"
result="$str1$str2"
echo $result

Output:

HelloWorld

2. Concatenating Strings with Numbers

You can also join strings with numbers without any problem:

name="User"
age=25
info="$name$age"
echo $info

Output:

User25

Bash treats numbers as strings when combining them with text.

3. Using Braces for Clarity

Braces {} help Bash understand where the variable ends, especially if you want to add text right after it:

file="document"
ext="txt"
filename="${file}.${ext}"
echo $filename

Output:

document.txt

Without braces, Bash might get confused if you write $filetxt.

4. Using the += Operator

You can add more text to an existing variable using +=:

greeting="Hello"
greeting+=" World"
echo $greeting

Output:

Hello World

This is handy when building a string step by step.


5. Using printf for Concatenation

printf allows more control, especially for formatting:

str1="Hello"
str2="Bash"
printf "%s %s\n" "$str1" "$str2"

Output:

Hello Bash

Use printf when you need formatted output or want to avoid surprises from echo.


6. Using Heredoc for Multi-line Strings

If you want to join multiple lines into one variable, Heredoc is the easiest way:

message=$(cat <<EOF
Hello,
This is a
multi-line string.
EOF
)
echo "$message"

Output:

Hello,
This is a
multi-line string.

Heredoc is useful for creating messages, scripts, or configuration files.

Conclusion

String concatenation is a basic but powerful tool in Bash scripting. By learning these six methods—placing strings together, combining numbers, using braces, the += operator, printf, and heredoc—you can handle most string operations in your scripts.

Start experimenting with these techniques in your own Linux scripts. Once you’re comfortable, you can combine them with variables, loops, and conditionals to create more advanced Bash programs.

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

Pystinger : Bypass Firewall For Traffic Forwarding Using Webshell

Pystinger is a Python-based tool that enables SOCKS4 proxying and port mapping through webshells. It…

1 day ago

CVE-Search : A Tool To Perform Local Searches For Known Vulnerabilities

Introduction When it comes to cybersecurity, speed and privacy are critical. Public vulnerability databases like…

1 day ago

CVE-Search : A Tool To Perform Local Searches For Known Vulnerabilities

Introduction When it comes to cybersecurity, speed and privacy are critical. Public vulnerability databases like…

2 days ago

How to Bash Append to File: A Simple Guide for Beginners

If you are working with Linux or writing bash scripts, one of the most common…

2 days ago

Mastering the Bash Case Statement with Simple Examples

What is a bash case statement? A bash case statement is a way to control…

2 days ago

How to Check if a File Exists in Bash – Simply Explained

Why Do We Check Files in Bash? When writing a Bash script, you often work…

4 days ago