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 ?
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.
Nice.
Mac OS X 10.12 works with following ...
#!/bin/bash
var1=bar
var2=foo
var3="$var1"sometext
echo $var3
Result
barsometext
var2 ("foo") in the result come from?barsometextvar2 has no impact to the result (var3) that is outputted and can hereby removed from your answer.Late for the party, my 2 cents for another solu., also works in zsh:
i=`date +%d%b`
val1="$i-i-*"
val1="$i-i-*"should work. Regarding your first question:val1="${Variable1}any string"(note the curlies).