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 ?
In JavaScript, we can assign strings to a variable and use concatenation to combine the variable to another string. To concatenate a string, you add a plus sign+ between the strings or string variables you want to connect. let myPet = 'seahorse'; console.
Bash also allows string concatenation using the += operator. Simply a+=b can be understood as a=a+b . Here, STR2 is appended at the end of STR1 , and the result is stored in the STR1 variable. To append multiple values, we can use a simple for loop.
String concatenation is the process of appending a string to the end of another string. This can be done with shell scripting using two methods: using the += operator, or simply writing strings one after the other.
To concatenate a string to an int value, use the concatenation operator. Here is our int. int val = 3; Now, to concatenate a string, you need to declare a string and use the + operator.
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.
Nice.
Mac OS X 10.12 works with following ...
#!/bin/bash var1=bar var2=foo var3="$var1"sometext echo $var3
Result
= barfoosometext
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With