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 Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

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