Live data from Hacker News

Shell script best practices, from a decade of scripting things

sharats.me

91–100 of 500 posts

Re: Shell script best practices, from a decade of scripting things

#91

> Use bash. Using zsh or fish or any other, will make it hard for others to understand / collaborate. Among all shells, bash strikes a good balance between portability and DX. I think fish is quite a bit different in terms of syntax and semantics (I'm not very familiar with it), but zsh is essentially the same as bash except without most of the needless footguns and awkwardness. zsh also has many more advanced featur…

As much as I love ZSH in my daily life, in sripting I HATE it for not having the "==" operator! >:((

Re: Shell script best practices, from a decade of scripting things

#92

Earlier 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?

Extension or not, .sh or .bash: definitely subjective.

That was the intention of my comment. Because the rest of the post (or most of it for sure) is not subjective.

Re: Shell script best practices, from a decade of scripting things

#93

I agree with basically all of this. A few more: The order of commandline args shouldn't matter. Env vars are better at passing key/value inputs than commandline arguments are. Process-substitution can often be used to avoid intermediate files, e.g. `diff some-file temp; diff some-file temp` If you're making intermediate files, make a temp dir and `cd` into that - Delete temp dirs using an exit trap (more reliable tha…

I'd strongly recommend using /tmp/* temp dirs to save your SSD and speed up things.

Re: Shell script best practices, from a decade of scripting things

#94
post #37

> 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…

My thumb rule is no extension if the script goes to the local bin folder and `.sh` otherwise. Beyond syntax highlighting, the extension also helps for wildcard matching for file operations (`ls`, `cp`, `for` loop, etc).

And this rule has been followed the majority(if not all) interpreted and scripting languages. The likes of Ruby, python and JS have multiple examples. Whatever executable is in your $PATH it won’t have an extension.

Not sure if this convention is actually documented anywhere.

Random examples:

- https://github.com/PyCQA/isort/blob/main/pyproject.toml#L100

- https://github.com/pypa/pip/blob/main/setup.py#L78

- https://github.com/11ty/eleventy/blob/master/package.json#L1...

Re: Shell script best practices, from a decade of scripting things

#97

Use the shell only if your script is mostly about calling other programs and filtering and redirecting their output. That's what the syntax of these languages is optimised for. As soon as you need any data manipulation (i.e. arrays, computation, etc.) it becomes a pain and Python is the much better fit.

AWK is just fine for data manipulation. And unlike python, you don't need to worry about whether it's installed and in what version.

Re: Shell script best practices, from a decade of scripting things

#100
post #84

Mostly agree, but I add more. 1. end all your lines C-style; this may save your life many times; 2. declare -is variables and -r CONSTANTS at the beginning, again, C-style; 3. print TIMESTAMP="$(date +%Y-%m-%d\ %H:%M:%S)"; where appropriate if your script logs its job; 4. Contrary to OPs reommendation I strongly try to stick to pure SH compatibility in smaller acripts so they can run on routers, TVs, androids and oth…

how do you make sure your scripts are SH compatible?
Post reply on HN