Concatenating variables in Bash

This question already has an answer here:

  • How to concatenate string variables in Bash 27 answers

  • Try doing this, there's no special character to concatenate in bash :

    mystring="${arg1}12${arg2}endoffile"
    

    explanations

    If you don't put brackets, you will ask bash to concatenate $arg112 + $argendoffile (I guess that's not what you asked) like in the following example :

    mystring="$arg112$arg2endoffile"
    

    The brackets are delimiters for the variables when needed. When not needed, you can use it or not.

    another solution

    (less portable : require bash > 3.1)

    $ arg1=foo
    $ arg2=bar
    $ mystring="$arg1"
    $ mystring+="12"
    $ mystring+="$arg2"
    $ mystring+="endoffile"
    $ echo "$mystring"
    foo12barendoffile
    

    See http://mywiki.wooledge.org/BashFAQ/013

    链接地址: http://www.djcxy.com/p/29124.html

    上一篇: 从Windows bash中形成的文本文件中读取内容

    下一篇: 在Bash中连接变量