1

What bash basic have I forgotten? I'm just trying to build a filename up in a script with VERSION and BUILD info...

So, dead-easy if hardcoded - this all makes sense:

VERSION=1.0.1
BUILD=45
NAME=Product-$VERSION-$BUILD.zip
echo $NAME
> Product-1.0.1-45.zip

But, what if the VERSION is provided via a (single) line in a file with contents just "1.0.1":

VERSION=$(<version.txt)
echo $VERSION
> 1.0.1
BUILD=45
NAME=Product-$VERSION-$BUILD.zip
echo $NAME
> -45.zip-1.0.1

?????? "-45.zip-1.0.1"?

Ok, I though it might be some left over newline or end of file character, so I've tried:

VERSION=$(cat version.txt)
VERSION=$(cat version.txt | tr -d '\n')
VERSION=$(sed -n '1p' version.txt)

Same effect. Using quotes around NAME="..." no joy (I had thought there was some interpolation going on...)

And, this is all on a Mac with:

bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin15)
Copyright (C) 2007 Free Software Foundation, Inc.

Thank you.

timlukins
  • 2,694
  • 21
  • 35
  • 3
    The file has DOS line endings; VERSION is actually `$'1.0.1\r`'. Remove the DOS line endings. – chepner Nov 05 '15 at 15:02
  • Yep - that was it! Forgot about \r (which of course on *nix systems is a _carriage return_ hence why it was returning to the start of NAME and overwriting it. Should have spotted that. Thanks. – timlukins Nov 05 '15 at 15:54

1 Answers1

2

Try:

VERSION=$(tr -d '\r' < version.txt)

as version.txt seems to have DOS line endings.

anubhava
  • 761,203
  • 64
  • 569
  • 643