Your issue is that the input text file is a DOS text file. This means that each line ends with a carriage-return character. When outputted, this character brings the cursor back to the start of the line, which means that when echo
outputs the space before &
, it will be written to the beginning of the line.
You should convert the text file to Unix text format using a tool like dos2unix
.
You also have issues with quoting in your shell code when you output the value of $line2
. Since you do not quote the variable expansion, the shell will split its value on spaces, tabs, and newlines to generate words. These words will also undergo filename globbing before being passed as individual arguments to echo
. This means that any tab or set of more than one consecutive space in your input would be converted to single spaces and that any globbing pattern in the input that happens to match one or more existing filenames would be replaced by those filenames. Try, for example, with a line of input with a single *
on it.
The echo
utility has a tendency to, under some circumstances, modify the data that it outputs (expanding \n
into literal newlines, etc.), so if you want to preserve the literal data of the input, you should be using printf
instead.
Reading text line by line in a shell loop is also a bit inefficient, and your task is better performed by tools such as sed
,
sed 's/$/ \&/' <"$1"
(replaces the end of the line with a space and an ampersand)
... or awk
,
awk '{ printf "%s &\n", $0 }' <"$1"
(outputs each line with space and &
added; could also have used print $0, "&"
).
See also: