How to Concatenate Strings in Bash Scripting


tl;dr: In bash scripting, we can concatenate strings or string variables using the += operator or using ${variable} within the string.


Example 1: using +=

    #!/bin/bash
    
    website_name="Code2care"
    website_name+=" - Lines of code for Change."
    
    concatenated_str="$website_name$website_tag_line"
    echo "$concatenated_str"
    
    Output:

    Code2care - Lines of code for Change.

    Concatenate Strings in Bash Scripting Example

Example 2: using ${variable}

    #!/bin/bash
    
    website_name='Code2care'
    website_tagline='Lines of code for Change.'
    
    concatenated_str="${website_name} - ${website_tagline}"
    echo "$concatenated_str"
    

    This approach is much cleaner!

    Example 2 to Concatenate Strings in Bash
-

Facing issues? Have Questions? Post them here! I am happy to answer!


Author: Rakesh
Author Info:

Rakesh is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

Copyright © Code2care 2023 | Privacy Policy | About Us | Contact Us | Sitemap