Home > database >  Split and display file line in bash
Split and display file line in bash

Time:01-26

I have a simple bash script and I don't understand the return value.

My script

#!bin/bash
string=$(head -n 1 test.txt)
IFS=":"
read -r pathfile line <<< "$string"
echo "left"$line"right"

And my test.txt

filepath:file content
others lines
...

I have this return on the console.

rightfile content 

The problem isn't when file only have 1 line.

I don't know why I don't have left value right to result.

CodePudding user response:

Your input file has MSWin line ends (\x0D\x0A). Therefore, \x0D becomes part of $line and when printed, it moves the cursor back to the beginning, so $line"right" overwrites it.

Run dos2unix or fromdos on the input file to fix it.

BTW, you don't need to quote left and right. Quoting the variable might be needed, though.

echo left"$line"right
  •  Tags:  
  • bash
  • Related