Bash Concatenate Strings
Quick Reference
$ X="String"
$ X+="Concatenation"
$ echo $X
# Output
StringConcatenationWhat 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!Concatenating a variable and a string:
$ X="String"
$ echo "${X}Concatenation!"
# Output
StringConcatenation!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: 4Concatenating multiple numbers:
$ X=314
$ Y=159
$ echo "$X$Y"
# Output
314159Concatenation 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.Related articles
Bash Comments
Comments will help make your scripts more readable
Reading User Input
Via command line arguments and prompting users for input
Curl Post Request
Use cURL to send data to a server
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.
Upload Files With curl
Learn how to upload a file to FTP, SFTP servers, Artifactory, and AWS S3 using the curl command.
How To Copy A Directory In Linux
Learn how to copy directories and their content in Linux using the cp command with options like -r for recursive copying, -i for interactive mode, and -a for preserving attributes.
Create Groups In Linux
Learn how to manually and automatically create and list groups in Linux.
How to Check the Size of Folders in Linux
Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.
Count Files in Linux
Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.
List Open Ports in Linux
Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.
Format Command Output In Linux
Learn how to filter and format the content of files and the output of commands in Linux using the awk command.