in Bash script by (10.9k points)
0 like 0 dislike
68 views

How I can to concatenate string variables in Bash script?


shell concat string

1 Answer

0 like 0 dislike
by (10.9k 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="Hello"
foo="${foo} World"
echo "${foo}"
> Hello World

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

a='Hello'
b='World'
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:

var=myscript
echo $var
> myscript

var=${var}.sh
echo $var
> myscript.sh

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.