in Bash script by (12.3k points)
0 like 0 dislike

How I can to concatenate string variables in Bash script?

shell concat string

1 Answer

0 like 0 dislike
by (12.3k points) edited by

In bash script don't have operator for concatenation. Therefore you can use variable in literal for concatinate it value like this:

foo="${foo} World"
echo "${foo}"
> Hello World

In general to concatenate two variables you can just write them one after another:

c="${a} ${b}"
echo "${c}"
> Hello World

Bash also supports a += operator as shown in this code:

A="X Y"
A+=" Z"
echo "$A"
> X Y Z

Quotes don't necessary. You can do this too:

echo $var
> myscript

echo $var

Your answer

Try to answer the question as detailed as possible.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.