looping through a file of tags seems to add a question mark

Viewed 41

I'm using bash and trying to loop through the lines of a file, to add git tags:

$ while read p; do  echo $p; git tag $p; done <NEW_TAGS.txt

Given the NEW_TAGS.txt has this content:

Test_V1_4.3
Test_V2_2.7

and each line ends with <CR><LF>, the result of the above line is this:

Test_V1_4.3
fatal: 'Test_V1_4.3?' is not a valid tag name.
Test_V2_2.7
fatal: 'Test_V2_2.7?' is not a valid tag name.

Where does the question marks come from? How do I remove them?

Alternatively, is there a better way to add a list of tags to git from a file?

1 Answers

The problem is newline character. Linuxes and Unixes use \n when dos (Windows) systems use \r\n . Use dos2unix to change newline character.

[Alex@Normandy tmp]$ cat NEW_TAGS.txt 
Test_V1_4.3
Test_V2_2.7
[Alex@Normandy tmp]$ file NEW_TAGS.txt 
NEW_TAGS.txt: ASCII text
[Alex@Normandy tmp]$ while read p; do  echo $p; git tag $p; done <NEW_TAGS.txt
Test_V1_4.3
fatal: tag 'Test_V1_4.3' already exists
Test_V2_2.7
fatal: tag 'Test_V2_2.7' already exists
[Alex@Normandy tmp]$ unix2dos NEW_TAGS.txt 
unix2dos: converting file NEW_TAGS.txt to DOS format ...
[Alex@Normandy tmp]$ file NEW_TAGS.txt 
NEW_TAGS.txt: ASCII text, with CRLF line terminators
[Alex@Normandy tmp]$ while read p; do  echo $p; git tag $p; done <NEW_TAGS.txt
Test_V1_4.3
fatal: 'Test_V1_4.3?' is not a valid tag name.
Test_V2_2.7
fatal: 'Test_V2_2.7?' is not a valid tag name.
Related