UNIX Hints & Hacks |
|||||||||||||||||||||||||||||||||||||
Chapter 1: Topics in Administration |
|
||||||||||||||||||||||||||||||||||||
|
Why not use echo instead of ls?
Flavors: All
Shells: Those that understand globbing.
Did you know that echo can list out a directory much like the ls command? The shell you use must understand globbing in order for this to work. The formatting of the information that results is really the only difference between the two. An output of ls yields a single or multiple column listing. When echo is used, the files are all spaced one right after the other.
% cd /var % ls * adm/ log/ named/ opt/ saf/ tmp/ audit/ lp/ news/ preserve/ spool@ uucp/ cron/ mail@ nis/ sadm/ statmon/ yp/ % echo * adm audit cron log lp mail named news nis opt preserve sadm saf spool statmon tmp uucp yp
In looking at the examples for the two commands, you can see right away that echo doesn't output any of the file description labels. The real ls command won't display the file description labels either. These labels help to identify the type of files, which are directories ( /), soft links ( @), or executables ( *).
So why do they show up on the ls command? These description labels appear when the -F argument is passed to ls. A lot of vendors and admins like to set an alias entry in the user startup login scripts as a convenience to help identify what the files are. Check the login script that you are using--.login, .profile, .cshrc, or .alias--and you will see an entry similar to
alias ls ls -CF
There might come a time when you will not be able to use the ls command. It might not even be accessible from miniroot. Script writing is easier without having to unalias the ls command all the time. Using echo displays a clean list of files within a given directory.
Hard system crashes or drives dying can bring systems down to the point where filesystems are so corrupted that they are unable to mount. When this occurs, at times the only way to see the system files is to use echo for displaying the files and directories.
In writing scripts for users or for the system, you never know whether the account that the script runs under has spurious alias definitions. Some users and admins can get creative with their aliases and pass multiple commands or pipe several commands together within an alias entry. To avoid having to set an unalias in your scripts, use the echo command. Here are some examples where echo might be used within scripts.
A variable definition:
list=`echo *`
To pass files through a loop:
for $list in `echo *` do source code done
Man pages:
alias, echo, ls
UNIX Hints & Hacks |
|||||||||||||||||||||||||||||||||||||
Chapter 1: Topics in Administration |
|
||||||||||||||||||||||||||||||||||||
|
© Copyright Macmillan USA. All rights reserved.