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

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!