In this tutorial, you will learn various methods for appending string variables in Bash.
There are multiple ways to append one string to another.
Simple variable append
Begin by declaring two string variables in the Bash script, which can be printed to the console using echo by enclosing the variables in double-quotes.
string1="Hello, "
string2='Welcome to w3schools.'
echo "$string1 $string2 "
You can also append without double quotes
echo $string1 $string2
Output:
Hello, Welcome to w3schools.
Another example involves concatenating a string to the same variable and printing it to the console:
result="My site is"
result="${result} w3schools"
echo "${result}"
Output:
My site is w3schools
This approach has pros and cons
- It is simple and easy to append strings.
- If you are appending multiple variables, it is less in terms of readability.
- Understanding the syntax may be initially difficult for new Bash users.
Use Shorthand Arithmetic Operator
The shorthand arithmetic operator (+=
) is commonly used in arithmetic to add a value to a variable. It can also be used for strings to append a string to a variable.
For example.
a+=1
is equivalent toa=a+1
in the case of numbers.str+="test"
will becomestr=str+"test"
in the case of strings.
Here is an example code
nums="One Two"
nums+=" three"
echo "$nums"
Output:
One Two three
Notes:
- Easy to append strings and readable, as arithmetic operators exist in every language.
- Not recommended and inefficient for larger strings.
Use printf command
printf
is used to format strings with various complex formatting options. We can use the printf
command to concatenate strings. The format is %s%s,
which appends two string variables.
str1="Hello, "
str2='Welcome to w3schools.'
output=$(printf "%s%s" "$str1" "$str2")
echo $output
Notes: It is not easy to understand the printf with formatted options
- Not easy to understand with the printf formatted options.
- Not recommended and inefficient for string append.
- Less readable for developers.
Using here string
Here strings
are a special syntax to pass a string to a command in a Bash script. They are used to pass an input string without using other sources, such as files. It allows passing a string to any Bash command from a file or a command line.
Syntax:
command <<< string
command: valid command
<<<
: is a here string operator
string is the input string
Here is an example
first="first "
second" second"
output="$first$(<<<" $second")"
echo $output
In this example, the second string is appended to the first string using the here string operator.
Notes:
- Another way to append strings simply.
- This approach is useful for passing strings to commands from a file or the command line only, even though it serves to append a string, it is less readable.
Conclusion
In this tutorial, you have learned how to concatenate string variables in multiple ways.
- Simple variable append and arithmetic operator(
+=
) are utilized for basic and straightforward string concatenation. - If you require more complex string processing alongside concatenation,
printf
is recommended.