bashshellscriptingconcatenation

How to concat variable and string in bash script


How to concat variable and string in bash script ?

val1 = Variable1 + "any string "

eg :

val1 = $i + "-i-*"

where i = 24thMarch

I want echo val1 :

24thMarch-i-*

What is proper proper to get the solution ?


Solution

  • Strings can be concatenated by simply creating a new string and expanding the values of the previous string variables directly within it.

    value="${variable} let's expand some more vars ${other_variable}"
    

    You must always wrap variable expansions in double quotes, otherwise your string will end at the first encountered whitespace character in the variables, which is a very common mistake.

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