JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

How to Concatenate Strings in Bash

Author:JIYIK Last Updated:2025/04/05 Views:

String concatenation is one of the most widely used operations in programming. It refers to connecting two or more strings by putting one string at the end of another string. To concatenate strings in Bash, we can write string variables one after another or use +=the operator to concatenate.

Bash string concatenation puts one string variable at the end of another string variable

We can concatenate strings by placing the string variables consecutively one after another.

STR1="Delft"
STR2="Stack"

STR3="$STR1$STR2"

echo "$STR3"

Output:

DelftStack

In the above example, we concatenate STR1and STR3and assign the concatenated string to STR3. Double quotes (" ")are used to prevent splitting or globbing issues.

We use echothe command to print the output.

Concatenate one or more variables with a string

STR1="Delft"

STR3="${STR1}-Stack"

echo "$STR3"

Output:

Delft-Stack

Here, {}it is used to separate string variables and string literals.

It concatenates a string variable STR1with a string literal .-Stack

Concatenate two or more strings together

We can place string variables and characters consecutively and concatenate two or more string variables together.

STR1="Delft"
STR2="-Stack"
STR3="Check them out!!"

STR4="${STR1}${STR2} has great programming articles.${STR3}"

echo "$STR4"

Output:

Delft-Stack has great programming articles.Check them out!!

Concatenate numbers and string characters

Bash does not distinguish between variable types when concatenating variables. They are interpreted as integers or strings, depending on the context.

STR1="FIVE-"
STR2=5

STR4="$STR1$STR2"

echo "$STR4"

Output:

FIVE-5

It concatenates the string variables FIVE-and .5

+=String concatenation using the operator

BashIt also allows +=string concatenation using the operator. Simple a+=bcan be understood as a=a+b.

STR1="Delft"
STR2="-Stack"

STR1+=$STR2

echo "$STR1"

Output:

Delft-Stack

Here, STR2is appended STR1to the end of and the result is stored in STR1the variable.

To append multiple values, we can use a simple forloop.

NUMS=""
for NUM in 'One' 'Two' 'Three' 'Four'; do
  NUMS+="${NUM} "
done

echo "$NUMS"

Output:

One Two Three Four 

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Move Multiple Files in Linux Bash

Publish Date:2025/04/06 Views:97 Category:OPERATING SYSTEM

In this article, we will explain how to move multiple files to the same directory in Linux. We will explain different methods such as typing multiple file names, using wildcard characters ( * ) to represent similar file names and/or identic

How to Compare Strings in Bash

Publish Date:2025/04/06 Views:190 Category:OPERATING SYSTEM

We can compare strings using various comparison operators and check if a string contains a substring using regular expressions. String comparison in Bash String comparison means checking whether the given strings are identical or not. Two o

How to Add Comments in Bash Scripts

Publish Date:2025/04/06 Views:123 Category:OPERATING SYSTEM

Comments are lines that are ignored by the interpreter and are only used to describe what is happening in the code or to provide insight into a particular block or line of code. Comments make it easier for the reader to understand the code.

tr Command in Linux Bash

Publish Date:2025/04/05 Views:54 Category:OPERATING SYSTEM

In Linux, we can use Bash scripts to perform string manipulation such as concatenation, truncation, and finding words in a text. tr This article will explain how to translate or remove characters using command in Linux Bash . tr Using comma

Differences between Bash Profile and Bashrc

Publish Date:2025/04/05 Views:188 Category:OPERATING SYSTEM

This article explains the difference between ~./bash_profile and files in Bash. ~/.bashrc What are startup files in Bash? Startup files are files that are executed after the shell is started. The startup files depend on the type of shell th

Differences between Sh and Bash

Publish Date:2025/04/05 Views:145 Category:OPERATING SYSTEM

This article explains what a shell is, find out which shell you are currently using, check a list of all available shells, and the difference between sh and . bash What is a Shell A shell is a computer program that accepts commands. It also

How to Append Text to a File Using Bash

Publish Date:2025/04/05 Views:83 Category:OPERATING SYSTEM

We can use the redirection ( ) operator and tee the command to append text to a file. We have to make sure we have enough permissions to add text to the file. If we don't have enough permissions, we may get a permission denied error. Use th

Configure Git Bash with Visual Studio Code

Publish Date:2025/03/31 Views:138 Category:Git

This article outlines the steps to configure Git Bash with Visual Studio Code on Windows. By default, VSCode uses PowerShell as the integrated terminal. We can configure VSCode to use Git Bash as the integrated terminal. Make sure you have

Open Git Bash on Mac

Publish Date:2025/03/31 Views:97 Category:Git

Managing Version Numbers in GitGit is the most popular and well-known free version control system used by developers to help them work on various programs safely and efficiently in a team. They can easily keep track of their own projects wi

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial