Live data from Hacker News

Things I Wish I'd Known About Bash

zwischenzugs.com

241–250 of 272 posts

Re: Things I Wish I'd Known About Bash

#241
post #157

Earlier quoted context omitted.

They are needlessly rude and mean at #bash. A bunch of scumbags, actually.

I think it's a result of constantly dealing with people who ask for help, receive good advice, and then ignore it

If that annoys then, they can always quit. Being rude in this situation is either a choice or lack of ability to cope with stress. Neither excuses being rude...

Re: Things I Wish I'd Known About Bash

#243

#0: If you're writing scripts that are destined for other users, use POSIX sh instead.

Do you know of any learning resources that are strictly POSIX sh instead of a specific shell? I'm looking to learn and it will be very helpful.

No need to learn strict POSIX shell. If you learn bash from a good guide and it should explicitly inform you about bash specific features. This way you can learn both at the same time.

On top of that you can use `checkbashisms` tool to lint your script against bash specific syntax.

Re: Things I Wish I'd Known About Bash

#244
post #232

Earlier quoted context omitted.

> As someone who has to occasionally modify 100+ line bash scripts written by Coworkers from Christmas Past which matched your spec in terms of what they had to do, please please just use Python (or similar). As someone who has inherited thousand-line shell scripts, and had to debug many 3rd party scripts, I stand by my assertion. > Yes, you will have a few extra lines but it will be vastly more readable and maintain…

subprocess.run does exactly what your wrapper does, subprocess.check_output returns stdout only and automatically throws exception on non-zero return code, this is the function you should be using 99% of the time. Those functions both accept a string as stdin-parameter.

Subprocess.run is Python 3+ only. If we're talking about replacing bash, Python 2.7 (possibly with 2.6 compatibility) is the more reasonable target. CentOS 7 and Debian 8 (I've not used 9 yet) still ship with Python 2.7.

Also, who is to say what I should be using "99% of the time?" Each problem has different constraints and different priorities.

Re: Things I Wish I'd Known About Bash

#245

Earlier quoted context omitted.

So it boils down to your personal taste.

For most workloads where bash is suited, so is sh. For workloads where sh is insufficient, bash probably is too and you should just use a higher level language. Chill. You don't have to hold bash so close to your heart.

I'm author of bash-modules project. It's my attempt to create set of libraries for easier scripting on bash in strict mode. Most bash libraries are not designed for strict mode, so I created my own. If you prefer sh over bash so much, you can help me to make it compatible with sh, or just fork it.

See https://github.com/vlisivka/bash-modules

Re: Things I Wish I'd Known About Bash

#246

#0: If you're writing scripts that are destined for other users, use POSIX sh instead.

Do you know of any learning resources that are strictly POSIX sh instead of a specific shell? I'm looking to learn and it will be very helpful.

Check out this presentation: https://youtu.be/olH-9b3VJfs

And the accompanying reference site: http://shellhaters.org/

Re: Things I Wish I'd Known About Bash

#247

Earlier quoted context omitted.

Exactly: GNU bash is on most GNU/Linux systems, but: * mac OS uses an ancient version of bash * Embedded systems use busybox sh * Android uses its own version of sh * None of the BSDs come with bash, since, y'know GNU licensing EDIT: Formatting

And to make things interesting Ubuntu uses dash as sh https://wiki.ubuntu.com/DashAsBinSh

`sh` is only supposed to be _a_ POSIX-compliant shell, it's not odd/interesting/confusing that Ubuntu uses dash any more than it is that something else uses bash.

The shebang line #!/bin/sh should only be used for POSIX-compliant scripts; if it needs {bash, dash, python, etc.} it should start #!/usr/bin/env {bash, dash, python, etc.}.

Re: Things I Wish I'd Known About Bash

#248
post #68

Earlier quoted context omitted.

The relevant point is that all systems running bash should also be POSIX compliant. So, taking some care to avoid bash-only constructs in a shell script can reap substantial gains in portability at fairly minor cost.

Why I should care about all systems? If targeted systems have bash4.x and GNU tools, why I should write scripts in sh? If you care about all systems, ANSI C is better choice, IMHO.

If you're targeting a limited set of systems and can reasonably assume the requirements won't change in the future, obviously use whatever tools you know will be available. I write Bash and Python all the time for those reasons.

But it's silly to think that there's nothing in between "complete control over target environment" and "use ANSI C so it can be compiled to any architecture and platform under the Sun." POSIX compliance will cover a lot. Try browsing /usr/bin on any Unix system you'll see plenty of use cases. I just looked at /usr/bin on my laptop and saw that mysqld_safe is a Bourne shell script, for just one example.

Re: Things I Wish I'd Known About Bash

#249
post #52

Using readline is a great thing to know about too. My favourite little-known readline command is operate-and-get-next: https://www.gnu.org/software/bash/manual/html_node/Miscellan... You can use it to search back in history with C-r and then execute that command with C-o and keep pressing C-o to execute the commands that followed that one in history. Very helpful for executing a whole block of history. For some reaso…

ctrl-o doesn't seem to do anything for me.

ctrl-r, find a command, press ctrl-o -> inserts ^o in the command and exits of ctrl-r.

Running bash 4.4.12(1) on Linux.

Re: Things I Wish I'd Known About Bash

#250
post #232

Earlier quoted context omitted.

subprocess.run does exactly what your wrapper does, subprocess.check_output returns stdout only and automatically throws exception on non-zero return code, this is the function you should be using 99% of the time. Those functions both accept a string as stdin-parameter.

Subprocess.run is Python 3+ only. If we're talking about replacing bash, Python 2.7 (possibly with 2.6 compatibility) is the more reasonable target. CentOS 7 and Debian 8 (I've not used 9 yet) still ship with Python 2.7. Also, who is to say what I should be using "99% of the time?" Each problem has different constraints and different priorities.

Please have a look at https://pythonclock.org/ and stop riding dead horses.
Post reply on HN