asked in Bash script by (8.9k points)
0 like 0 dislike
26 views

How I can to concatenate string variables in Bash script?


shell concat string

1 Answer

0 like 0 dislike
answered by (8.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.

Related questions

0 like 0 dislike
1 answer 12 views
0 like 0 dislike
1 answer 10 views
0 like 0 dislike
1 answer 12 views
0 like 0 dislike
1 answer 29 views
0 like 0 dislike
1 answer 11 views
0 like 0 dislike
1 answer 16 views
0 like 0 dislike
1 answer 15 views
0 like 0 dislike
1 answer 46 views
...