1

I have a script that bumps a version of a package.json. I can bump the version with this script, but I'm having problems printing a variable, where I'm expecting "1.2.3", but get "1 2 3".

#!/bin/bash 

# Split string by '.'
IFS='.'
read -r -a array <<< "1.1.3"
array[1]=$((array[1]+1))

VERSION="${array[0]}.${array[1]}.${array[2]}"
echo $VERSION
# 1 2 3

VERSION=`printf "%i.%i.%i" "${array[0]}" "${array[1]}" "${array[2]}"`
echo $VERSION
# 1 2 3

echo $(jq -r '.version = "'"$VERSION"'" ' package.json)
# packaje.json -> "version": "1 2 3"

What am I missing here?

dsicari
  • 590
  • 6
  • 13

2 Answers2

3

This is because you've set IFS for the whole script, rather than just the one command which needs it. Try IFS='.' read […] instead.

Also, if you Use More Quotes™ you won't have this issue, because the output won't be split by $IFS.

l0b0
  • 55,365
  • 30
  • 138
  • 223
  • Work perfectly. I changed to `IFS='.' read -r -a array <<< "1.1.3"` and solved it. As @harshit kumar answered you could `unset IFS` or `IFS=''`. – dsicari Apr 13 '21 at 13:20
1

You're just missing quotes. The shell is splitting VERSION on . before it passes the arguments to echo.

$ VERSION=1.2.3
$ echo $VERSION
1.2.3
$ IFS=.
$ echo $VERSION
1 2 3
$ echo "$VERSION"
1.2.3
William Pursell
  • 204,365
  • 48
  • 270
  • 300