15

in order to understand another answer:

find / -type d -print0 | while read -r -d ''; do ls -ltr "$dir" | sed '$!d'

the first step is to understand the usage of the option -r of the read command.

First, I thought, it would be sufficient to simply execute

man read

to lookup the meaning of the -r option , but I realized the man page does not contain any explanation for option at all, so I googled for it got some read -t , read -p examples but no read -r .

23

There is no stand-alone read command: instead, it is a shell built-in, and as such is documented in the man page for bash:

read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p
prompt] [-t timeout] [-u fd] [name ...]
[...]
       -r     Backslash does not act as an escape character.  The back‐
              slash is considered to be part of the line.  In  particu‐
              lar,  a  backslash-newline pair may not be used as a line
              continuation.

So, to summarize, read normally allows long lines to be broken using a trailing backslash character, and normally reconstructs such lines. This slightly surprising behavior can be deactivated using -r.

  • 6
    A demo: str="a\bc"; read x <<< "$str"; read -r y <<< "$str"; echo "$x"; echo "$y" – glenn jackman Mar 27 '15 at 12:47
  • @glennjackman That's great, it's even more striking if you type str="a` and then hit Enter before pasting in the rest of the command starting with b`. – ErikE Jun 20 '18 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.