lsコマンドで必ず改行するようにする
素のlsコマンドは必ずしも改行されるとは限らない
code:bash
$ ls /
Applications Volumes home tmp
Library bin installer.failurerequests usr
Network cores net var
System dev private
Users etc sbin
解決策: -1オプションを付けて明示的に標準出力にリダイレクトすればいい
code:bash
$ ls -1 / >&1
Applications
Library
Network
System
Users
Volumes
bin
cores
dev
etc
home
installer.failurerequests
net
private
sbin
tmp
usr
var
解説
lsコマンドのmanを見ると以下のように書いてある
-1 (The numeric digit one''.) Force output to be one entry per line. This is the default when output is not to a terminal.
つまり、lsの結果の何かしらのファイルにリダイレクトすればファイル(ディレクトリ)ごとに必ず改行を入れることができる
しかし、ls打つごとにゴミファイル作りたくない(標準出力したい)ので、上記のコマンドでは代わりに標準出力のファイルディスクリプタ宛てにリダイレクトするようにしている。