Concat strings in a shell script

BashString Concatenation

Bash Problem Overview


How can I concat strings in shell? Is it just...

var = 'my';
var .= 'string';

?

Bash Solutions


Solution 1 - Bash

How about this:

var="${var}string"

Solution 2 - Bash

It depends on the shell, but since question was tagged bash:

var='my'
var=$var'string'

Solution 3 - Bash

No. For various reasons.

# most sh-compatible shells
var="my"
var="$var string"

# advanced shells
var="my"
var+=" string"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBenView Question on Stackoverflow
Solution 1 - BashcnicutarView Answer on Stackoverflow
Solution 2 - BashdldnhView Answer on Stackoverflow
Solution 3 - BashIgnacio Vazquez-AbramsView Answer on Stackoverflow