53

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

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

?

codeforester
  • 39,467
  • 16
  • 112
  • 140
Ben
  • 60,438
  • 111
  • 314
  • 488
  • 5
    FYI variables in bash can't have spaces around the `=`. It has to be next to the name and the value. – Daenyth Feb 25 '12 at 16:10

3 Answers3

87

How about this:

var="${var}string"
cnicutar
  • 178,505
  • 25
  • 365
  • 392
30

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

var='my'
var=$var'string'
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
dldnh
  • 8,923
  • 3
  • 40
  • 52
11

No. For various reasons.

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

# advanced shells
var="my"
var+=" string"
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358