4

The following works, but I don't want the space that it returns:

read input
file= "$input"
file= "$file ins.b" # how to get rid of the space here?
echo "$file"

This outputs 'file ins.b' I don't want the space between file and ins.b

If I don't leave that space in the code it returns only '.b'. What can I do to resolve this problem?

codeforester
  • 39,467
  • 16
  • 112
  • 140
  • Review [How can I concatenate string variables in Bash](http://stackoverflow.com/questions/4181703/how-can-i-concatenate-string-variables-in-bash). It isn't quite a duplicate because it wants the space that you don't want, but some of the answers cover the cases you need covered. (For example, you could use `file="$file""ins.b"` or `file="$file"ins.b`; these are covered alternatives, as well as the simple `file="${file}ins.b"`.) – Jonathan Leffler Jun 13 '14 at 16:40

4 Answers4

4

Append like:

file="${file}ins.b" 

If you don't use braces then it treats fileins as a variable and expands it. Since, it's probably not set it just prints .b.

Related: When do we need curly braces in variables using Bash?

Community
  • 1
  • 1
P.P
  • 117,907
  • 20
  • 175
  • 238
2

In bash you can also reference variables like ${file}. So this should work for you:

file="${file}ins.b"
Manny D
  • 20,310
  • 2
  • 29
  • 31
2

You don't need to expand the old value at all; bash has a += operator:

file+="ins.b"
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
1
file="${file}ins.b"

or

file=$file"ins.b"
mb14
  • 22,276
  • 7
  • 60
  • 102