Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Bash Concatenate Strings - DesignLinux

Nov 10 2020
designlinux 0 Comments
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.

Related

Tags: bash, terminal

Bash until Loop

Prev Post

Zeit – A GUI Tool to Schedule Cron and At Jobs in Linux

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 852
  • 1,055,624

DesignLinux.com © All rights reserved

Go to mobile version