4 ms·
Knew most of those, not much interested in "shell plugins". One thing that many people miss are parameter expansions. Those are in Posix, so pretty much any ba
by mhd 6y ago
Knew most of those, not much interested in "shell plugins".
One thing that many people miss are parameter expansions. Those are in Posix, so pretty much any bare shell should be able to use that, too. So dash/bash/zsh/ksh…
echo Hello ${PLANET:-World}
to have a default if you're not sure a variable is set. Replace "-" with "=" to actually set it, not just use it for this statement.
Or removing suffixes:
filename=/tmp/foobar.tar.gz
echo ${filename%.*} # -> /tmp/foobar.tar
echo ${filename%%.*} # -> /tmp/foobar
Saves you some useless sed/awk.
I still wish the Linux shells (bash/zsh) would start to finally get some more features from friggin' ksh93.
- olsgaarddk 6y agoThat's a really neat way of trimming the file extension, something that can end up being quite tedious to do with SED. Do you know of a cheatsheet that describes what all the percentage sign means and other things you can do with parameter expansions?
- Symbiote 6y agoThe old way to remove a file extension is using basename: basename -s .png example.png example
- mhd 6y agoAlso strips the path and you have to know the extension name(s). For my examples, "cut" would be appropriate. But those substitutions tend to work in anything POSIX, not just bash/zsh.
- Tiksi 6y agoThe bash hackers wiki has a pretty good list of the bash/posix one, tldp does too, but I find it less straight forward. https://wiki.bash-hackers.org/syntax/pe https://wiki.bash-hackers.org/syntax/pe https://tldp.org/LDP/abs/html/parameter-substitution.html https://tldp.org/LDP/abs/html/parameter-substitution.html The zsh docs are good for the zsh specific expansions, but more man page than cheatsheet, though they do have an intro doc on it that's more cheatsheet like but less complete. http://zsh.sourceforge.net/Doc/Release/Expansion.html http://zsh.sourceforge.net/Doc/Release/Expansion.html http://zsh.sourceforge.net/Intro/intro_12.html http://zsh.sourceforge.net/Intro/intro_12.html
- Symbiote 6y agoecho $filename:r is the shorter, Zsh way to strip a file extension. What am I missing from Ksh93?
- mhd 6y agoHmm, let me check, I think zsh does have associative arrays and floating point, two features that bash took quite a bit longer to get (and which aren't in the bash in OS X). So at least that's okay. Compound and active variables are quite convenient in longer scripts. As are namerefs, but I think zsh might have something similar enough.