Live data from Hacker News

Shell script best practices, from a decade of scripting things

sharats.me

131–140 of 500 posts

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

#131
Relying on errexit to save one from disaster is also often fatal, except for surpassingly simple scripts. While inside of many different kinds of control structures, the errexit is disabled, and usually just provides a false sense of security.

For someone who knows errexit can't be trusted, and codes defensively anyway, it's fine.

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

#132

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

sh and bash feel pretty primitive after learning PowerShell.

sh and bash feel a lot more sane and reasonable after learning some PowerShell. PowerShell is mostly not-a-shell.

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

#133

Earlier quoted context omitted.

sh and bash feel pretty primitive after learning PowerShell.

They don't feel primitive, they are primitive.

Yep, just as a screwdriver is. For certain jobs, that's all you need.

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

#134
post #126

Earlier quoted context omitted.

I see your point, but you can just as easily run "sh script" although that does imply that you already know that it's a shell script (obviously you wouldn't just run something from the internet without checking it first).

> obviously you wouldn't just run something from the internet without checking it first This died a long time ago with the pervasive use of NPM and PIP and the likes. Most developers probably run a lot of random unchecked shit all the time with local user privileges today without a blink. Somehow people are ready for all this, but are still afraid to run a random shell script from the internet. I guess this fear is o…

It's also fairly common to use a docker container that someone else built without having a look at it

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

#138
post #126

Earlier quoted context omitted.

I see your point, but you can just as easily run "sh script" although that does imply that you already know that it's a shell script (obviously you wouldn't just run something from the internet without checking it first).

> obviously you wouldn't just run something from the internet without checking it first This died a long time ago with the pervasive use of NPM and PIP and the likes. Most developers probably run a lot of random unchecked shit all the time with local user privileges today without a blink. Somehow people are ready for all this, but are still afraid to run a random shell script from the internet. I guess this fear is o…

If you're committed to being an idiot no amount of rules of thumb will save you. Not understanding what you use is one facet of being committed to that.

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

#139

Earlier quoted context omitted.

If you download a script then running "sh script.sh" is a lot quicker and easier than a chmod followed by ./script.sh. You can of course also type "bash script.sh", but I don't always have it installed on every system, and the .bash extension just clarifies it. For things in my PATH I drop any suffixes like that.

I see your point, but you can just as easily run "sh script" although that does imply that you already know that it's a shell script (obviously you wouldn't just run something from the internet without checking it first).

The .sh extension told me it's a shell script of some kind.

I don't check everything I download from the internet; I don't think anyone does. It depends on what it is, where I'm getting it from, where I'm running it, etc. There are certainly some things I will review carefully, but other things I give just a quick check to see it's not in complete shambles, and others I barely check at all. I typically run the latest Vim from master, do I check every patch to see if after 30 years Bram finally sneaked in a crypto miner or password stealer? Do the people who package Vim for the Linux distros?

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

#140
If 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.

Post reply on HN