Bash Concatenate Strings
Prianka Subrahmanyam
Software Engineer, Modern Treasury
Published: 12/1/2023
Quick Reference
$ X="String"
$ X+="Concatenation"
$ echo $X
# Output
StringConcatenation
Run in Warp
What is Concatenation?
Concatenation is the process of joining two strings together into one result. Some common use cases include creating a string containing multiple variables, and formatting messages containing user-defined inputs.
Basic Concatenation
In Bash, when variables or strings are written one after another, they automatically concatenate. In the following examples, the code will echo the same output to the terminal.
Concatenating variables:
$ X="String"
$ Y="Concatenation!"
$ echo "${X}${Y}"
# Output
StringConcatenation!
Run in Warp
Concatenating a variable and a string:
$ X="String"
$ echo "${X}Concatenation!"
# Output
StringConcatenation!
Run in Warp
The curly braces are used to “protect” the names of the X and Y variables from other characters in the string. They make it clear to Bash that you are using a variable with a specific name. Use double quotes for strings containing variable names - this tells Bash to interpolate, or substitute in, the values of the variables.
Concatenation with multiple variable types
Bash infers the type of variables depending on how they are used. So, this means we can use integer or boolean variables in string concatenation as well.
Concatenating an integer and a string:
$ X="The number four: "
$ Y=4
$ echo "$X$Y"
# Output
The number four: 4
Run in Warp
Concatenating multiple numbers:
$ X=314
$ Y=159
$ echo "$X$Y"
# Output
314159
Run in Warp
Concatenation with += operator
You can use the += operator to append to the end of a string variable.
$ X="Pi is the number "
$ X+=3.14159
$ X+= " and it is very handy."
$ echo "$X"
# Output
Pi is the number 3.14159 and it is very handy.
Run in Warp
Written by
Prianka Subrahmanyam
Software Engineer, Modern Treasury
Filed Under
Related Articles
Bash If Statement
Learn how to use the if statement in Bash to compare multiple values and expressions.
Bash While Loop
Learn how to use and control the while loop in Bash to repeat instructions, and read from the standard input, files, arrays, and more.
POST JSON Data With Curl
How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.
Use Cookies With cURL
Learn how to store and send cookies using files, hard-coded values, environment variables with cURL.
Loop Through Files in Directory in Bash
Learn how to iterate over files in a directory linearly and recursively using Bash and Python.
How To Use sudo su
A quick overview of using sudo su
Generate, Sign, and View a CSR With OpenSSL
Learn how to generate, self-sign, and verify certificate signing requests with `openssl`.
How to use sudo rm -rf safely
We'll help you understand its components
How to run chmod recursively
Using -R is probably not what you want
Run Bash Shell In Docker
Start an interactive shell in Docker container
Curl Post Request
Use cURL to send data to a server
Reading User Input
Via command line arguments and prompting users for input