51

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

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

?

codeforester
  • 34,080
  • 14
  • 96
  • 122
Ben
  • 58,238
  • 105
  • 295
  • 471
  • 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

85

How about this:

var="${var}string"
cnicutar
  • 172,020
  • 25
  • 347
  • 381
30

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

var='my'
var=$var'string'
Mariusz Jamro
  • 29,116
  • 24
  • 107
  • 151
dldnh
  • 8,877
  • 3
  • 37
  • 51
11

No. For various reasons.

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

# advanced shells
var="my"
var+=" string"
Ignacio Vazquez-Abrams
  • 740,318
  • 145
  • 1,296
  • 1,325