Live data from Hacker News

Shell script best practices, from a decade of scripting things

sharats.me

371–380 of 500 posts

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

#371

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

Make sure you check out abbreviations

Like aliases but they expand in-place, so auto completion friendly, easily modifiable, etc. Love them

$ abbr s sudo

$ s -> sudo

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

#372
Other tips for working with files:

always quote filenames, because you never know if there's a space in them.

filenames with dashes or periods will kill you

prepend current directory file manipulation filenames with "./", because the file might start with a period or dash

Dashes in filenames still might kill you, especially if you pass those to another command

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

#373

Earlier quoted context omitted.

I tried to write a PowerShell script to recursively scan and find files/folders older than a certain date, but kept hitting problems with the length of the path/filenames. As a complete PowerShell noob, I'm sure I was trying to do it the wrong way, but after a few attempts, I gave up and install cygwin instead.

You can enable long path support in Windows to have paths up to 32,767 characters long https://learn.microsoft.com/en-us/windows/win32/fileio/maxim...

It was a few years back, so probably before Windows included that ability (Windows 10 onwards?)

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

#374

Hands down, shell scripting is one of my all time favorite languages. It gets tons of hate, e.g. "If you have to write more than 10 lines, then use a real language," but I feel like those assertions are more socially-founded opinions than technically-backed arguments. My basic thesis is that Shell as a programming language---with it's dynamic scope, focus on line-oriented text, and pipelines---is simply a different p…

I sometimes regret I never learned to "really" write shell scripts. I stumbled across Perl early on, and for anything more complex than canned command invocation(s) or a simple loop, I usually go for Perl.

There is something to be said in favor of the shell being always available, but Perl is almost always available. FreeBSD does not have it base of the base system, but OpenBSD does, and most Linux distros do, too.

But it is fun to connect a couple of simple commands via pipes and create something surprisingly complex. I don't do it all the time, but it happens.

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

#375
post #145

Earlier quoted context omitted.

The issue I have with powershell is the extreem verbosity. But that's just a personal thing and not something that I can realy blame the language.

The verbosity might be annoying at first, but it does make long pipelines easier to read. This enforced verbosity makes it easier to read than a linux pipeline using some arcane `qw -eRTy` command with no rhyme or reason.

Not to me, powershell is a pain to write and read with my heavy dyslexia. But again, this is personal.

Pretty sure there are other dyslectics who will find that it helps them. So I guess this all depends on person to person

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

#376

Earlier quoted context omitted.

>However, if you instead organize all your data in a format that's sympathetic to line-oriented processing on stdin-stdout, then shell will work with you instead of against. Not even that is necessary. Just use structured data formats like json. If you are consuming some API that is not json but still structured, use `rq` to convert it to json. Then use `jq` to slice and dice through the data. dmenu + fzf + jq + curl…

Do you have examples of concurrent use-cases that xargs and parallel don't satisfy? I discovered parallel recently and was blown away by how much it improves things. I've only really used it in basic scenarios so far, just wondering where its limitations are.

running a bash function with its own private variables in parallel. without having to export it.

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

#377

Hands down, shell scripting is one of my all time favorite languages. It gets tons of hate, e.g. "If you have to write more than 10 lines, then use a real language," but I feel like those assertions are more socially-founded opinions than technically-backed arguments. My basic thesis is that Shell as a programming language---with it's dynamic scope, focus on line-oriented text, and pipelines---is simply a different p…

[deleted]

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

#378

Earlier quoted context omitted.

echo is unreliable; I agree. Instead, I use "paranoid" printf with leading double dash: prinf -- "fmt str here..." "$carefully" "$quoted" "$args"

printf -- "fmt str here..." "${carefully}" "${quoted}" "${args}"

What is the difference?

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

#379

Earlier quoted context omitted.

I feel like I do this every three years then proceed to never use it. Then I read a post on hn and think about how great it could be; rinse and repeat

yeah thats exactly right. it may only take an hour to learn, but every time i need to use awk it seems like i have to spend an hour to re-learn its goofy syntax.

alas this is true, I never correctly recall the order of particular function args as they are fairly random, still beats the alternative of having to continually internalize entire fragile ecosystems to achieve the same goal.

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

#380
post #151

Earlier quoted context omitted.

sh and bash feel pretty primitive after learning PowerShell.

I tried PowerShell, hated it. The idea of manipulating objects instead of text streams is interesting, and avoids most of the footguns sh/bash have, but you also lose in flexibility. One reason is that there are thousands of command line tools in the UNIX ecosystem that process text streams and are designed to work with shells like bash. You have much less options when you are processing PowerShell objects. Note: I t…

   ls -File | % { $fh = Get-FileHash $_ SHA1; if ($_.Name -match $fh.hash) {$_} }
Post reply on HN