In PHP, strings are concatenated together as follows:
$foo = \"Hello\";
$foo .= \" World\";
Here, $foo
becomes \"Hello World\"
I prefer to use curly brackets ${}
for expanding variable in string:
foo="Hello"
foo="${foo} World"
echo $foo
> Hello World
Curly brackets will fit to Continuous string usage:
foo="Hello"
foo="${foo}World"
echo $foo
> HelloWorld
Otherwise using foo = "$fooWorld"
will not work.
I do it this way when convenient: Use an inline command!
echo "The current time is `date`"
echo "Current User: `echo $USER`"
Bash also supports a +=
operator as shown in this code:
A="X Y"
A+=" Z"
echo "$A"
output
X Y Z
If you want to append something like an underscore, use escape (\)
FILEPATH=/opt/myfile
This does not work:
echo $FILEPATH_$DATEX
This works fine:
echo $FILEPATH\\_$DATEX
There's one particular case where you should take care:
user=daniel
cat > output.file << EOF
"$user"san
EOF
Will output "daniel"san
, and not danielsan
, as you might have wanted.
In this case you should do instead:
user=daniel
cat > output.file << EOF
${user}san
EOF
var1='hello'
var2='world'
var3=$var1" "$var2
echo $var3