String concatenation is a fundamental aspect of Bash scripting, allowing the combination of string literals, variables, and command outputs to form more complex strings. This capability is especially useful in loops, where dynamic string creation is often required. Whether you’re a beginner or an advanced user, understanding how to effectively concatenate strings in Bash is a valuable skill.
In this tutorial you will learn:
- Basic string concatenation techniques
- Concatenating strings with variables
- Concatenating strings with command outputs
- Using loops to concatenate strings dynamically

Understanding String Concatenation in Bash
Before diving into the examples, it’s important to understand that in Bash, strings can be concatenated simply by placing them next to each other. This applies to string literals, variables, and command substitutions. Bash does not have a specific concatenation operator like some other programming languages. Instead, it relies on the context and placement of strings and variables.
- Basic String Concatenation: Concatenating two string literals directly.
#!/bin/bash string1="Hello" string2="World" concatenatedString="$string1$string2" echo $concatenatedString
Basic String Concatenation - Concatenation with Space: Adding a space between concatenated strings.
#!/bin/bash string1="Hello" string2="World" concatenatedString="$string1 $string2" echo $concatenatedString
Concatenation with Space - Concatenating String and Variable: Combining a string literal with a variable.
#!/bin/bash greeting="Hello" name="Alice" message="$greeting, I am $name!" echo $message
Concatenating String and Variable - Concatenation in a Loop: Dynamically building a string in a loop.
#!/bin/bash prefix="Count: " for i in {1..5}; do echo "$prefix$i" done
This Bash script illustrates the use of string concatenation within a loop, a common scenario in many scripting tasks. The script efficiently combines a fixed string with a variable integer that changes in each iteration of the loop. It starts with the
#!/bin/bash
shebang line, signifying that it should be executed in a Bash shell.The script sets up with a variable
prefix
assigned the string “Count: “. This string serves as the static part of the message that will be prepended to the dynamic part in the loop.The
for i in {1..5}; do ... done
construct is a simple loop that iterates over a range of numbers from 1 to 5. In each iteration, the value ofi
changes, taking on the next number in the sequence.Inside the loop, the
echo "$prefix$i"
command demonstrates string concatenation in action. In each iteration, the script concatenates theprefix
variable with the current value ofi
. There’s no special syntax required for concatenating a string with a number in Bash; placing them adjacent to each other in a command or variable assignment suffices.As the loop runs, the script prints a series of lines. Each line begins with “Count: “, followed by the current iteration number (1 through 5). This results in the following output:
Concatenation in a Loop - Concatenating Command Output: Including the output of a command in a string.
#!/bin/bash text="Current directory: " currentDir=$(pwd) echo "$text$currentDir"
Concatenating Command Output - Multi-line Concatenation: Creating a multi-line string.
#!/bin/bash line1="This is the first line." line2="This is the second line." multiLine="$line1\n$line2" echo -e $multiLine
This Bash script demonstrates the creation of a multi-line string by concatenating two separate strings with an embedded newline character. It’s a useful technique when you need to construct text that spans multiple lines, such as in formatted outputs or multi-line messages. The script starts with
#!/bin/bash
, indicating that it’s meant to be run in a Bash shell environment.The first two lines define two variables,
line1
andline2
, each holding different text strings: “This is the first line.” and “This is the second line.”, respectively. These variables store the individual lines of text that will be combined into a single multi-line string.The critical part of this script is the line
multiLine="$line1\n$line2"
. Here, a new variablemultiLine
is created to store the result of concatenatingline1
, a newline character\n
, andline2
. The newline character\n
is the key element here, as it serves as a line break, ensuring thatline1
andline2
appear on separate lines in the output.The last line of the script,
echo -e $multiLine
, prints the concatenated multi-line string. The-e
flag in theecho
command is essential, as it enables the interpretation of backslash escapes. Without-e
, the\n
would be treated as a regular text string rather than a newline character.When this script is run, the output will be:
Multi-line Concatenation - Advanced Concatenation with Printf: Utilizing printf for formatted string concatenation.
#!/bin/bash string1="Hello" string2="World" printf -v formattedString "%s - %s\n" "$string1" "$string2" echo $formattedString
Advanced Concatenation with Printf - Concatenation with Arrays: Concatenating elements of an array.
#!/bin/bash array=("apple" "banana" "cherry") for fruit in "${array[@]}"; do echo "Fruit: $fruit" done
This Bash script highlights the use of string concatenation within a loop, specifically in the context of iterating over an array. It combines elements of the array with a static string in each iteration, a common task in many scripting scenarios for generating lists or processing arrays. The script begins with the standard
#!/bin/bash
shebang line, indicating that it should be executed in a Bash environment.The script starts by defining an array
array=("apple" "banana" "cherry")
. This array holds a list of fruit names, and each element represents a different fruit.The
for fruit in "${array[@]}"; do ... done
loop is where the script iterates over each element in the array. The"${array[@]}"
syntax is used to access all elements of the array. In each iteration of the loop, the variablefruit
takes the value of the current array element.Inside the loop, the
echo "Fruit: $fruit"
command demonstrates the concatenation of a static string with each array element. The string “Fruit: ” is prefixed to each fruit name stored in thefruit
variable. This concatenation is performed in every iteration of the loop, resulting in the following output:Concatenation with Arrays
Conclusion
String concatenation in Bash scripting is a versatile tool, enabling scripts to dynamically create and manipulate strings. By mastering concatenation techniques, you can efficiently handle various scripting scenarios, from simple message displays to complex data processing. The examples provided here are just the tip of the iceberg, encouraging you to explore and experiment with Bash’s powerful string manipulation capabilities.
Frequently Asked Questions (FAQs) About String Concatenation in Bash Scripting
- What is string concatenation in Bash scripting?
String concatenation in Bash is the process of joining two or more strings together to form a single string. - How can I concatenate two strings in Bash?
To concatenate two strings, place them next to each other. For example,concatenatedString="$string1$string2"
. - How do I add a space when concatenating strings?
Include a space within the quotes during concatenation, like"$string1 $string2"
. - Can I concatenate a string with a variable?
Yes, place the variable next to the string:message="$greeting, $name"
. - How can I concatenate strings in a loop?
In a loop, you can concatenate strings for each iteration. For instance,echo "$prefix$i"
inside a for loop. - Is it possible to concatenate a string with the output of a command?
Yes, use command substitution:currentDir=$(pwd)
and then concatenate it like"$text$currentDir"
. - How to create a multi-line string in Bash?
Use newline characters (\n
) in your string:multiLine="$line1\n$line2"
. - What is the purpose of using
printf
in string concatenation?
printf
allows formatted concatenation, which is useful for complex string constructions. - Can I concatenate elements of an array with a string?
Yes, iterate over the array and concatenate each element:echo "Fruit: $fruit"
within a loop. - How do I handle concatenation of strings with special characters?
Use quotes to ensure special characters are treated as part of the string. - Can concatenated strings contain variables and command outputs simultaneously?
Yes, you can mix variables and command outputs in a single concatenated string. - How do I append a string to an existing variable?
Usevariable+="additional string"
to append to the variable. - Is it necessary to initialize a variable before concatenating strings to it?
It’s good practice, but not strictly necessary. Uninitialized variables are treated as empty strings. - Can I use concatenation to build file paths dynamically?
Yes, concatenation is often used to construct dynamic file paths in scripts. - Are there any limitations to string concatenation in Bash?
Bash does not inherently limit concatenation, but complex concatenations can be less readable and harder to maintain.