Brenden Carvalho
Brenden Carvalho

Reputation: 127

concatenate two variables in bash

I wanted to concatenate two variables, but it seems that there is some overwriting.

#!/bin/bash
NUMBER1=$(seq 1 900 | sort -R | head -1)
FIRST=$(sed -n ''$NUMBER1'p' names.txt)
echo ${FIRST}
echo "${FIRST}${NUMBER1}"

Where names.txt is a list of names. For example when I run this code, I get the output as,

Gregoria

159goria

Notice $FIRST was partially overwritten by $NUMBER1 .

The correct output should have been,

Gregoria

Gregoria159

can someone please help me ? Thanks

Upvotes: 0

Views: 207

Answers (1)

rici
rici

Reputation: 241721

Your names.txt file has Windows line-endings, CR-LF. The CR (carriage return) is not being recognized as part of the new-line sequence by sed, so it stays on the end of the line Gregoria<CR>; consequently, the next characters get overprinted at the beginning of the line.

Use dos2unix or some equivalent to fix the line endings.

Upvotes: 2

Related Questions