> Use the .sh (or .bash) extension for your file. It may be fancy to not have an extension for your script, but unless your case explicitly depends on it, you’re probably just trying to do clever stuff. Clever stuff are hard to understand. I don't agree with this one. When I name my script without extension (btw, .sh is fine, .bash is ugly) I want my script to look just like any other command: as a user I do not care…
There are use cases where you don't have execute privileges. In those cases the .sh-extension makes it clear that you can do `bash script.sh`. If you don't use an extension you wouldn't easily see that that was an option.
Shell script best practices, from a decade of scripting things
141–150 of 500 posts
Re: Shell script best practices, from a decade of scripting things
#142> For copy-paste: if [[ -n "${TRACE-}" ]]; then set -o xtrace; fi > People can now enable debug mode, by running your script as TRACE=1 ./script.sh instead of ./script.sh. The above "if" condition will set xtrace even when user explicitly disables trace by setting TRACE=0. A correct way of doing this would be: if [[ "${TRACE-0}" == "1" ]]; then set -o xtrace; fi
Re: Shell script best practices, from a decade of scripting things
#143> Use the .sh (or .bash) extension for your file. It may be fancy to not have an extension for your script, but unless your case explicitly depends on it, you’re probably just trying to do clever stuff. Clever stuff are hard to understand. I don't agree with this one. When I name my script without extension (btw, .sh is fine, .bash is ugly) I want my script to look just like any other command: as a user I do not care…
In my setup, I use aliases or functions to have short/mnemonic names for commands. But the files on disk must always have proper extensions like .sh to quickly see what they are.
Re: Shell script best practices, from a decade of scripting things
#144There's a bug in his template. He suggests to `set -eu`, which is a good idea, but then immediately does this: if [[ "$1" =~ ^-*h(elp)?$ ]]; ... If the script is given no arguments, this will exit with an unbound variable error. Instead, you want something like this: if [[ "${1-}" =~ ^-*h(elp)?$ ]]; then
Re: Shell script best practices, from a decade of scripting things
#145If you are on Windows or Linux, Powershell is a decent scripting language that comfortably replaces Shell for scripted task running. The commands are vastly more readable and you get an okay experience with branches. I'd also say that in most cases Python is also a better choice, especially when you use the ! syntax.
But that's just a personal thing and not something that I can realy blame the language.
Re: Shell script best practices, from a decade of scripting things
#146Re: Shell script best practices, from a decade of scripting things
#147Earlier quoted context omitted.
> .bash is ugly "Ugly" is subjective. If I encountered a file with that extension, I'd assume it uses Bash-specific features and that I shouldn't run this script with another shell.
Subjetive, indeed. But unless I am missing something, the interpreter to be used should be determined by the shebang within the script though?
Re: Shell script best practices, from a decade of scripting things
#148Earlier quoted context omitted.
zsh is, historically, a step from csh-like interactive shells in the direction of Bourne/ksh compatibility. It's easy to get the impression that zsh is a newer development than bash, but they're actually contemporary—bash rode on the popularity of GNU in the 90s, despite being a "small evolution" (frankly, a step back) compared to the ksh lineage.
It certainly didn’t hurt the popularity of Bash by having it be the default shell on tens of millions of Macs for so many years. I’m aware that ZSH has been the default shell since Catalina. Started using fish a month ago and really liking it.
I think being the de-facto default on Linux as part of "GNU plus Linux" has more to do with it.
Re: Shell script best practices, from a decade of scripting things
#149About that "#!/usr/bin/env bash" business - are there any systems out there that have "/usr/bin/env", but do not have "/bin/bash"?
Re: Shell script best practices, from a decade of scripting things
#150Earlier quoted context omitted.
Bash extensions are cool to have in the interpreter. I really don't think we need more than the basic POSIX shell for most scripts. I once wrote a tar replacement in it, with a restricted YAML generator and parser and a state machine — don’t judge me, I think I was manic — and the result was weirdly beautiful.
The lack of arrays, dicts and local variables when trying to be POSIX compliant becomes quickly annoying when writing big programs though. There are of course workarounds to deal with those but I wish we didn't have to use them.