3 ms·
Zsh (and to a lesser extent Bash) has support for arrays, which you can use to represent long command line invocations in relative safety. E.g. instead of this
by nerdponx 4y ago
Zsh (and to a lesser extent Bash) has support for arrays, which you can use to represent long command line invocations in relative safety. E.g. instead of this sort of madness:
#!/bin/sh
set -eu
# Argh I can't write comments in between lines... just email me if you have q's: nerdponx@mail.example.net
exec /usr/sbin/boop \
--enable-foobars \
--this-is-a-really-long-option \
--sound=quack:.:loader=org.example.soundfactory-lib.SoundFactory.wrapped_loader \
--sound=woof:.:loader=net.quibbler.QuibblerWoofMaker \
--jvm-opts='-Xms512M -Xmx1G -Xquux=1234 -Xbootclasspath:.'
But you can preserve what's left of your brain cells without switching to a "non-shell" language if you use Zsh, and then you don't have to sweat over shell quoting and parameter expansion either:
#!/usr/bin/env zsh
# Enable "strict" features for typo-safety
emulate zsh
setopt err_exit pipe_fail warn_create_global warn_nested_var no_unset
boop_args=(
# We always want foobars, why isn't this on by default?
--enable-foobars
# See: https://stackoverflow.com/questions/4973
--this-is-a-really-long-option
# Use the good sounds! Please see README for obtaining these deps.
--sound=quack:.:loader=org.example.soundfactory-lib.SoundFactory.wrapped_loader
--sound=woof:.:loader=net.quibbler.QuibblerWoofMaker
# Trust me, don't touch these.
--jvm-opts='-Xms512M -Xmx1G -Xquux=1234 -Xbootclasspath:.'
)
exec /usr/sbin/boop ${(@)boop_args}
- Groxx 4y agoaaah, that's a great idea for interleaving comments, I'll have to use that! thanks! previously I've done this, which works, but is a bit....... yeah. some command \ `# did you know comments work inside backticks like this?` \ --complex_flag_needing_descriptions=... \ `# literally any ffmpeg flag` \ --lol no now if only shellcheck would support zsh....
- remram 4y agoFor completeness, the other option is: CMD="some command" # Some comment CMD="$CMD --verbose --complex-flag=..." # Other stuff CMD="$CMD --lol=\"no thanks\"" eval $CMD
- Groxx 4y agoyeah, I just don't like dealing with the possibly-multiple-levels of escaping that sometimes requires :\ there are ways to deal with that of course, but it's kinda unavoidably more complex.
- Beltalowda 4y ago> now if only shellcheck would support zsh Since the default zsh behaviour is much more reasonable (e.g. just $foo won't do globbing or word splitting) and you don't really need POSIX compatibility if you're going to use zsh-specific features, the need for shellcheck is hugely less than what you need with bash or POSIX sh.
- nerdponx 4y agoStill, it would be pretty handy. For example a good Zsh linter could warn you if you use a Bashism that isn't valid in Zsh, or it could switch to Posix mode if you set `emulate sh`, or warn you when an autoload function file is missing its function call at the bottom, etc.
- Beltalowda 4y agoSure, but that's "only" useful warnings/errors of that kind that are useful for any language, rather than footguns that work on first approximation and then don't ($*, unquoted $@, unquoted $var, stuff like that). A zsh LSP would be nice as well.
- nerdponx 4y agoVery fair! I bet you could use the C "module" interface to write the Zsh LSP in Zsh itself...
- Beltalowda 4y agoThere's https://github.com/zsh-users/zsh-syntax-highlighting https://github.com/zsh-users/zsh-syntax-highlighting which does some parsing, but I never looked at the details of it (I just know it exists).