How to concat variable and string in bash script

后端 未结 3 1399
野的像风
野的像风 2020-12-15 21:51

How to concat variable and string in bash script ?

val1 = Variable1 + \"any string \"

eg :

val1 = $i + \"-i-*\"


        
相关标签:
3条回答
  • 2020-12-15 22:24

    Nice.

    Mac OS X 10.12 works with following ...


    #!/bin/bash
    
    var1=bar
    var2=foo
    var3="$var1"sometext
    echo $var3
    

    Result

    = barfoosometext

    0 讨论(0)
  • 2020-12-15 22:31

    Late for the party, my 2 cents for another solu., also works in zsh:

    i=`date +%d%b`
    

    val1="$i-i-*"

    0 讨论(0)
  • 2020-12-15 22:35

    Strings are concatenated by default in the shell.

    value="$variable"text"$other_variable"
    

    It's generally considered good practice to wrap variable expansions in double quotes.

    You can also do this:

    value="${variable}text${other_variable}"
    

    The curly braces are useful when dealing with a mixture of variable names and strings.

    Note that there should be no spaces around the = in an assignment.

    0 讨论(0)
提交回复
热议问题