3 ms·
> “ls” is never gonna change. It has changed in the past and it will change in the future. "ls" will always list files in some way, but it is the only thing y
by GuB-42 5y ago
> “ls” is never gonna change.
It has changed in the past and it will change in the future.
"ls" will always list files in some way, but it is the only thing you can take for granted. You don't really know what kind of decoration you will get, how spaces and non-ascii characters will appear, the language, the order, etc... You may also find yourself with a weird environment variable your user has set that makes ls look better in his interactive shell and breaks your script.
Generally, I avoid "ls" in scripts for that reason, but many commands have the same kind of problem. Your GNU script may break on busybox or BSD (happened to me many times). And good luck knowing what /bin/sh means, it may be bash, it is supposed to be POSIX, that's all we know.
- justaj 5y agoThat's why it's a good rule of thumb to just stick with POSIX as defined by the POSIX standard [0] 0: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V...
- GuB-42 5y agoTotally agree, tough I am not sure that all shells that pretend to be POSIX really are. But it is a minefield, it is way to easy to sneak-in a bashism because you are so sure it is POSIX that you didn't check, made worse by the fact that if you are writing shell scripts, you are probably using an much more liberal interactive shell all day long. And your script will work and no one will complain until you try to run it on another system. It is not entirely unlike client-side web technologies, where you have to check for browser compatibility. For the web, it is a recognized problem and there are plenty of solutions to deal with it: jquery, babel, etc... I am not aware of such tools for shell scripts, or maybe just analyzers that check for posix compliance.
- justaj 5y agoThat's easily solved with http://www.shellcheck.net/ http://www.shellcheck.net/