3 ms·
The single biggest shell script mistake is not handling whitespace in file names correctly, and it's almost impossible to do correctly if you have weird file na
by barrkel 13y ago
The single biggest shell script mistake is not handling whitespace in file names correctly, and it's almost impossible to do correctly if you have weird file names: embedded newlines, leading and trailing spaces, embedded tabs. Embedded quotes can be tricky too, especially if you're writing a script that generates a script.
That bit, writing a script that generates a script, happens surprisingly often in bash. It's cheaper to pipe a stream to sed that converts it into a shell command than it is to iterate over all the lines, and individually pluck out the arguments for the commands you want to execute. Leaving the script as something that outputs shell commands also lets you inspect what it does before committing to it (by piping it to bash).
- lifthrasiir 13y agoRelated: Fixing Unix/Linux/POSIX filenames. http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html http://www.dwheeler.com/essays/fixing-unix-linux-filenames.h... (HN discussion at https://news.ycombinator.com/item?id=6644955 https://news.ycombinator.com/item?id=6644955)
- yogsototh 13y agoThe problem with file name is the main reason I use zsh. for fic in **/*(.); do doSomethingWith $fic done will take way more time than using find, but will work even with files with stranges chars.
- q_revert 13y agoand another reason to use zsh is the incredibly nice looping syntax, for example, this is the same as for fic (**/*(.)) doSomethingWith $fic I find the fact that it fits in a single line makes me much more likely to use it in everyday shell use.. though I'd still revert to bash for scripting