Bash Script: For-Loop Over Newline ... Syntax Explained?

A frequently asked question, both on SO and elsewhere, is using bash for-loop to step through each line of output. I know the answer, and I enjoyed using the following years:

IFS=$'\n' 

However, I am a little puzzled by the syntax used. I had a good search / google, but due to the large number of characters that are used / ignored by search engines in the above example, I did not find an explanation.

I also recently noticed this in some outputs from another script. I am having problems echoing from the netcat result. Upon closer examination, it turns out that the exit from nc was in a similar format:

 $'Some_Output_Here\r` 

I had to disconnect \r , but was not sure of the best way to do this, I ended up using egrep -o ([0-9|aZ]*) . Any better ideas?

+4
source share
2 answers

The Bash manual explains this citation method, which it calls ANSI-C Quoting .

+2
source

After Michael edited the original post, I think it makes more sense.

I think the person who posted should look for removal of carriage returns. which should be enough with

 sed -e 's/^M//g' 

There are many other ways to remove control non-printable characters:

All control characters:

 egrep -v "[[:cntrl:]]" sed -r "s/[[:cntrl:]]//g" 

non-printable characters

 tr -dc '[:print:]' 
+1
source

Source: https://habr.com/ru/post/1433311/


All Articles