While creating a bash script, I found that this code ls
puts all files on one line:
pi@raspberrypi:~/ptlrestinterface$ ls
update.sh web.config MyApp.runtimeconfig.json
still ls | head -n1
still prints only the first file:
pi@raspberrypi:~/ptlrestinterface$ ls | head -n1
update.sh
I would expect it to output the entire line, not the first file.
While piping the output through hexdump
, I saw ls
always put out a 0a
after each entry, however on the console, it places them next to each other.
Apparently ls
has some knowledge of the console (or the console of ls
). How does this work, and where could I find documentation on this behaviour?
-t
option. This also works without any options (I updated the answer). My point is that the|head -n1
takes the first filename, and not the entire line.ls -C | head -n 1
# should work?.ls | something
# it notices stdout is not a terminal but another command, so it will default to one entry per line.ls -C
: will force multiple entry per lines.