Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»Bash Shell»How to Concatenate two string variables in Bash Script

    How to Concatenate two string variables in Bash Script

    RahulBy RahulNovember 27, 20141 Min Read

    Command:



    str3=”$str1 $str2″


    The above example command will concatenate values of str1 and str2 stings and store it in third variable str3. Make sure there should not be any space before or after the assignment ( = ) operator.

    Example 1:

    It is simply write two or more strings variable one after one for concatenating them together.

    $ str1="Hello"
    $ str2="World"
    $ str3="$str1 $str2"
    $ echo $str3
    > Hello World
    

    Example 2:

    It is not required that both string should be variable, We can use one variable and other direct sting to concatenate them.

    $ str1="Hello"
    $ str2="$str1 World"
    $ echo $str2
    > Hello World
    

    Example 3:

    We can simply write string variable one after one using $ symbol to concatenate two strings

    $ str1="Hello"
    $ str2="World"
    $ str3=$str1$str2
    $ echo $str3
    > HelloWorld
    
    bash concatenate string
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Assign Password to User in a Shell Script
    Next Article How to check if string contains substring in JavaScript

    Related Posts

    How to Replace String in JavaScript

    Updated:June 13, 20222 Mins Read

    How to Backup Website to Amazon S3 using Shell Script

    Updated:March 24, 20222 Mins Read

    Bash Printf Command

    Updated:December 23, 20212 Mins Read

    Bash Sequence Expression (Define Range)

    Updated:January 12, 20222 Mins Read

    Creating Menu in Shell Script (Linux Select Command)

    Updated:May 31, 20222 Mins Read

    Bash Break and Continue

    Updated:November 9, 20213 Mins Read

    1 Comment

    1. arlie jenks on January 10, 2018 11:14 pm

      I have the following script that works in AIX but not in red-hst/linux.
      /pbin/${8}.ksh $[1} ${2} /pswork/sp.${1}.step120.outfile ${4} ${5} ${6}
      1=LX22BILN
      2=22
      3=BILL
      4=22GR
      5=22BILL
      6=22BILL
      7=NONE
      8=PR22GRPH
      Because the positional position of /pswork/sp.${1}.step120.outfile is 3, it substitues BILL for ${1}
      I t should be substituting LX22BILN for ${1}

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • What is CPU? – Definition, Types and Parts
    • What is the /etc/aliases file
    • What is the /etc/nsswitch.conf file in Linux
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.