• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

Bash Concatenate Strings

Written by Admin, Updated On November 9, 2020
bash, terminal
Bash Concatenate Strings

In any programming language the concatenation is the commonly used string operations. String concatenate is used to joining the two or more strings together by appending one to end of another string. In this tutorial, you will learn how to concatenate strings in Bash.

Concatenating Strings#

It’s very easy to concatenate multiple string variables by writing them one after another:

VAR1="Hey,"
VAR2=" Dude!"
VAR3="$VAR1$VAR2"
echo "$VAR3"

In the last line the echo will print the concatenated string:

Hey, Dude!

You can also concatenate one or more variable with literal strings:

VAR1="Hey, "
VAR2="${VAR1}Dude"
echo "$VAR2"
Hey, Dude!

Here, in above example VAR1 variable is protected from surrounding characters by enclosing with curly braces. You must enclose the variable in curly braces ${VAR1} when it is followed by another valid variable-name.

It is best practice to use the double quotes around the variable name to avoid any word splitting issues. When you need to use backslash characters use the single quotes instead of double quotes.

Bash does not segregate variables by “type”, variables are treat as integer or string depending on contexts. You can also concatenate variables that contain only digits.

VAR1="Hey, "
VAR2=5
VAR3=" Dude"
VAR4="$VAR1$VAR2$VAR3"
echo "$VAR4"
Hey, 5 Dude

Concatenate Strings with the += Operator#

You also can concatenate the strings in bash using the += operator by appending variables or literal strings to a variable.

VAR1="Hey, "
VAR1+=" Dude"
echo "$VAR1"
Hey, Dude

The following example is using the += operator to concatenate strings in bash for loop :

Below is the given an example to concatenate strings in bash for loop:

VAR=""
for STATE in 'Florida' 'California' 'Texas' 'Alaska'; do
  VAR+="${STATE} "
done

echo "$VAR"
Florida California Texas Alaska

Conclusion#

You learned how to concatenate the string variables in bash scripting. You can also check our guide about comparing strings.

If you have any questions or feedback, feel free to leave a comment.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article Bash until Loop
Next Article   How to Install AnyDesk on Ubuntu 20.04

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook