简体   繁体   中英

bash shell script concatenate string with period char

I am trying to create following string

Beta-3.8.0

but shell script always omits the . period char no matter what I do.

echo "$readVersion"

if [ -z $readVersion ]
then
    echo "readVersion is empty"
    exit 1
fi;

IFS=.
set $readVersion
newVersion=$(echo "$2 + 1" | bc)
newBranch="Beta-$1.$newVersion.$3"
echo $newBranch

prints:
3.8.0
Beta-3 9 0

I have also tried

newBranch='Beta-'$1'.'$newVersion'.'$3
or
newBranch="Beta-{$1}.{$newVersion}.{$3}"

although this seems printing the right value echo "$1.$newVersion.$3" why not variable doesnt work ?

I need the variable to use later on in the script...

You can save and restore the IFS once you are done.

oldIFS=$IFS
IFS=.
set $readVersion
newVersion=$(echo "$2 + 1" | bc)
IFS=$oldIFS
newBranch="Beta-$1.$newVersion.$3"
echo "$newBranch"

Or you can quote when printing:

echo "$newBranch"

The former is a better idea IMO since it conveys your intention and would make the rest of the code use the "correct" IFS. The latter just circumvents the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM