1

I'm writting a script in Bash. In it I read a long list of names using readarray. I want to create another array with the names of the first array concatenated with ".git". That is, I have a file like this:

first
second
third

and want to have an array with data

first.git
second.git
third.git

but I get:

.gitt
.gitnd
third.git

Yes, the last one always work. If I don't use readarray but a local variable like this:

files=("first" "second" "third")

everything works. This is the minimal script that reproduces the issue:

readarray repo_names < repoNames.txt 

for it in ${repo_names[@]}; do
  echo "${it}.git"
done

test=("first" "second" "third")
for it in ${test[@]}; do
  echo "${it}.git"
done

result:

user@debian:~/git/code/transfer$ ./tmp2.sh 
.gitt
.gitnd
third.git
first.git
second.git
third.git

Why is it appending the string, overwritting it, instead of concatenating?

cauchi
  • 1,463
  • 2
  • 17
  • 45
  • That was it! it had windows line endings. if you write that as an answer I'll mark it as the correct one – cauchi Aug 05 '21 at 13:27
  • You do not even need a loop: `files=("first" "second" "third"); printf '%s.git\n' "${files[@]}"` – Léa Gris Aug 05 '21 at 14:22
  • 1
    Or `files=( "${files[@]/%/.git}" )` to update all the array entries at once with a [parameter expansion](https://wiki.bash-hackers.org/syntax/pe). – Charles Duffy Aug 05 '21 at 14:29

0 Answers0