4 ms·
Slightly off topic, is there a nice/modern way to deal with filenames that can have spaces in bash scripts? The only solutions I have seen use tons of quotes a
by ape4 5y ago
Slightly off topic, is there a nice/modern way to deal with filenames that can have spaces in bash scripts? The only solutions I have seen use tons of quotes are are unreadable/unmaintainable.
- chubot 5y agoYou can set IFS='' globally, which disables splitting. (And you have to make sure it stays that way, since there are some idioms which modify IFS) Although there is the "empty elision" case related to splitting: if x='', then $x is different than "$x", even if IFS=''. You could also use zsh which doesn't do word splitting, but does "empty elision". (I'd say zsh is a better interactive shell than a scripting language, but I've seen it used on occasion.) Or you can use OSH, which will run your bash script as is. And then you can opt into shopt -s simple_word_eval or the group shopt -s oil:basic, and EVERYTHING will be "unmolested", quotes or not: http://www.oilshell.org/blog/2021/04/simple-word-eval.html http://www.oilshell.org/blog/2021/04/simple-word-eval.html
- OskarS 5y agoJust be careful to quote filename variables. Don't write this: cp $FILENAME $TARGET Write this: cp "$FILENAME" "$TARGET" It's a bit annoying, but it's not that bad, and certainly not unmaintainable.
- enriquto 5y agothe true (and unpopular) solution is to disallow spaces in filenames
- pama 5y agoI strongly recommend the book Unix Power Tools for all such questions. Quotes become easier to grok once you think of different ways to remove meaning of special characters. Basically, 1) backslash removes special meaning of the next character, 2) surrounding by double quote removes special meaning of all characters except backslash, dollar, and backtick, and 3) surrounding by single quotes removes all special meanings.
- ebeip90 5y agoCoffee is for closers^W quoters. ABQ. Always. Be. Quoting. Also, you should be checking your Bash scripts with shellcheck, which will tell you when to do this with ~99% accuracy. (For the uninitiated: https://www.youtube.com/watch?v=GrhSLf0I-HM https://www.youtube.com/watch?v=GrhSLf0I-HM)
- l0b0 5y agoI'm afraid quotes are the way to go. The alternatives are basically escaping (which looks even worse) or using a different language. The book also mentions a few more tricks you probably want to know about for dealing with the really tricky stuff, like filenames starting with a hyphen or ending with a newline.