|
|
|
|
|
by vbezhenar
2093 days ago
|
|
No, the reason is to aid human. $ touch 'a b' c d
$ ls
'a b' c d
Without quotes you would get something like $ ls
a b c d
and that would be confusing.It wouldn't help shell scripts that assumed no whitespaces in file names anyway. You would get something like "'a", "b'", "c", "d" tokens which make no sense anyway. The only sane way to iterate over files that I'm aware of is to use something like find . -type f -print0 | while read -d '' fname; do echo "fname: '$fname'"; done
But that's bashism... |
|
Splitting on spaces, expanding asterisks, shell scripting is a minefield. You would basically never want any splitting to happen. Applications like reading a CSV by splitting using IFS are dirty hacks, that break with the slightest additional file parsing complexity (escapes, quoting etc).
In Python you can just do `os.listdir('.')` and it will actually do what you want. No 5 layers of "oh, actually" and "yes, but what if", which inevitably happen in threads discussing the simplest shell operations. It just works as intended. If you only want the files and not the directories, you can do `filter(os.path.isfile, os.listdir('.'))`.
There is no reason to use shell scripts for anything more complex than a few lines or for one-off interactive work.