• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

Bash Concatenate Strings

Thumbnail for Prianka SubrahmanyamPrianka Subrahmanyam

Prianka Subrahmanyam

Software Engineer, Modern Treasury

Published: 12/1/2023

About Terminus

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

Thumbnail for Prianka SubrahmanyamPrianka Subrahmanyam

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
Thumbnail for Gabriel ManricksGabriel Manricks

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.

Bash

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.

BashUnixLinux
Thumbnail for Neeran GulNeeran Gul

Use Cookies With cURL

Learn how to store and send cookies using files, hard-coded values, environment variables with cURL.

Bash

Loop Through Files in Directory in Bash

Learn how to iterate over files in a directory linearly and recursively using Bash and Python.

BashPython
Thumbnail for Razvan LudosanuRazvan Ludosanu

How To Use sudo su

A quick overview of using sudo su

LinuxUnixBash
Thumbnail for Razvan LudosanuRazvan Ludosanu

Generate, Sign, and View a CSR With OpenSSL

Learn how to generate, self-sign, and verify certificate signing requests with `openssl`.

BashLinuxUnix
Thumbnail for Razvan LudosanuRazvan Ludosanu

How to use sudo rm -rf safely

We'll help you understand its components

BashLinuxUnix
Thumbnail for Neeran GulNeeran Gul

How to run chmod recursively

Using -R is probably not what you want

LinuxBashUnix
Thumbnail for Brett TerpstraBrett Terpstra

Run Bash Shell In Docker

Start an interactive shell in Docker container

DockerBash
Thumbnail for Razvan LudosanuRazvan Ludosanu

Curl Post Request

Use cURL to send data to a server

BashUnixLinux
Thumbnail for Zev StravitzZev Stravitz

Reading User Input

Via command line arguments and prompting users for input

BashLinuxUnix
Thumbnail for Amit JotwaniAmit Jotwani

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac
Request demo
Thumbnail for null