0

I have a file called test.txt. the content of the has two line/rows in it
i-08ece3afc
i-0001db77b

so I have done like this but

#!/bin/bash
set -x
head -1 /home/test.txt > /test1.txt  #it has now one row in test1.txt
 file="/home/test1.txt"
name=$(cat "$file")
echo "name=$name"

but the out is

 head -1 /home/test.txt
+ file=/home/test1.txt
++ cat /home/test1.txt
+ name=$'i-08ece3afc\r'
+ echo $'name=i-08ece3afc\r'
name=i-08ece3afc

file1="${name}"

file1= $'i-08ece3afc\r'
and giving ERROR as failed to satisfy constraint: Member must satisfy regular expression pattern: ^[a-zA-Z0-9_:-]{1,400}$ '

I can see the variable name is correct in the output but why name ,echo file1 is giving trailing as $ ' & leading \r'

I am expecting the output as

 head -1 /home/test.txt
+ file=/home/test1.txt
++ cat /home/test1.txt
+ name=i-08ece3afc
+ echo name=i-08ece3afc
name=i-08ece3afc

file1="${name}"

file1= i-08ece3afc
Sathya
  • 1
  • 1
  • 1
    Your file has DOS line endings; POSIX tools don't consider the carriage return in the CR/LF line ending to have any special meaning and do not strip it as a result. – chepner Jul 26 '23 at 14:44
  • The output you posted doesn't come from the code you posted (e.g. there is nothing in your code that would print `file1=` twice). Also, can you run `file /home/test.txt` and post the output here so we can learn about your line endings? – David Grayson Jul 26 '23 at 14:52

0 Answers0