Strange “echo” behavior in shell script

前端 未结 3 1538
梦毁少年i
梦毁少年i 2021-01-18 18:59

I want to print the content that I have obtained from the split of an array in this way:

string=\"abc test;ABCtest.it\"

IFS=\';\' read -a array <<<         


        
3条回答
  •  -上瘾入骨i
    2021-01-18 19:43

    Somehow, the value of $string ends with a carriage return. What you are actually echoing is

    echo -ne "\nABCtest.it\r,abc test"
    

    That carriage return causes the cursor to move to the beginning of the current line before printing the text that follows it, overwriting the url. Either remove the carriage return with

    string=${string#?}
    

    or fix how string is set in the first place (I suspect you are reading from a file that uses DOS line endings, in which case you can convert the file with dos2unix.)

提交回复
热议问题