0

Not a duplicate of question.

I am trying to make semvar pattern (1.0.0) from array having 1,0,0 as elements and create a git tag git tag $var.

Error:- The above solution is just not working for . only, it is giving 1 0 0, space separated.

4 13 0 fatal: too many params 

Error with git tag command.

Approach:-

var=$( IFS=$'.'; echo "${versionArray[*]}" ) echo $var git tag $var 

Please suggest a better way to do it.

5
  • What is the exact input and exact output required? Commented Jul 3, 2019 at 15:40
  • 2
    What is $IFS in your main shell? If it's . then echo $var will print 1 0 0; but echo "$var" should print 1.0.0. Note for the same reason echo $IFS is not a right way to print $IFS. Commented Jul 3, 2019 at 15:45
  • 1
    Also, just IFS=. should be enough to set the value. . has no special meaning under the ANSI C escaping rules. Commented Jul 3, 2019 at 15:47
  • $IFS is not printing anything. @KamilMaciorowski . Commented Jul 3, 2019 at 15:54
  • @KamilMaciorowski echo "$var" worked for me. Please put it as answer, I'll accept it. Commented Jul 3, 2019 at 16:00

1 Answer 1

2

My guess is the shell that executes echo $var and git tag $var uses IFS=. as well. Note var=$(IFS=.;…) cannot change the variable value in the current shell, so this unexpected IFS must have been set earlier, probably during some trials and errors.

If I'm right, the variable named var holds the expected value with dots, e.g. 1.0.0. But when you retrieve it like this

echo $var 

the current IFS is used to split 1.0.0 into words, so echo receives three separate arguments: 1, 0, 0. And because echo prints its arguments with single spaces in between, you were under impression the value of var was 1 0 0.

This line

echo "$var" 

doesn't rely on IFS. I expect it will show you 1.0.0.


A general solution is to double-quote variables. You should have done it anyway:

echo "$var" git tag "$var" 

Also pay attention to IFS. Now it should be clear echo $IFS won't show you what the variable is; echo "$IFS" is better. In addition these methods are useful:

printf '%s' "$IFS" | hexdump -c printf '%s' "$IFS" | hexdump -C printf '%s' "$IFS" | xxd 
2
  • Very well. Yes, he variable named var holds the expected value with dots, I am able to write in another file properly. Thanks for the time and efforts. Commented Jul 3, 2019 at 16:52
  • 1
    I'd also recommend against leaving nonstandard values of IFS active any more than necessary, since (as we're seeing) it can have unexpected effects. var=$(IFS=.;…) is fine, since the change is isolated to a subshell performing one operation. In other cases, when you need to change it, it's best to make the change, do what you need the nonstandard value for, and then immediately set it back to normal. Commented Jul 3, 2019 at 20:04

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.