top | item 30345487

Bash Pitfalls

106 points| pujjad | 4 years ago |mywiki.wooledge.org | reply

32 comments

order
[+] dang|4 years ago|reply
Related:

Bash Pitfalls - https://news.ycombinator.com/item?id=24401085 - Sept 2020 (111 comments)

Bash Pitfalls - https://news.ycombinator.com/item?id=10068567 - Aug 2015 (38 comments)

Bash Pitfalls - https://news.ycombinator.com/item?id=6790169 - Nov 2013 (54 comments)

Common Bash Pitfalls - https://news.ycombinator.com/item?id=869560 - Oct 2009 (25 comments)

Bash Pitfalls - https://news.ycombinator.com/item?id=52700 - Sept 2007 (1 comment)

[+] notreallyserio|4 years ago|reply
I often see your "related" comment at the top of these posts and they mostly seem to match what shows up when you click "past". Would it make sense to automatically add a prominent link to the most recent or most commented on post in the header so you wouldn't have to copy & paste the links as comments?
[+] HenrikB|4 years ago|reply
(Disclaimer: I'm the co-developer)

Together with GitHub user xPMo, I created a Shellcheck REPL tool (https://github.com/HenrikBengtsson/shellcheck-repl) that validates your Bash commands using ShellCheck _before_ they are evaluated. For example,

$ words="lorem ipsum dolor" $ echo $words ^-- SC2086: Double quote to prevent globbing and word splitting.

It was a toy project at first, but since I've learned so much about Bash from using it, I now have it enabled all the time.

[+] ttanev|4 years ago|reply
A very happy and grateful user here, jumping in here just to thank you for the time and efforts to create this one! It saved me many hours of debugging after stupid midnight-coding-sessions mistakes. We even have it in our coding-style internal guidelines for some time now.
[+] actually_a_dog|4 years ago|reply
Neat. I like it.

I do have one suggestion: you have it ignoring "SC2154: 'var' is referenced but not assigned" by default, which makes sense on the command line because you're often not assigning and then referencing the same variable in a single command. But, I think it would be useful to have a similar warning like "SCREPL01: 'var' is not defined in the local environment," or something, which you might implement in shellcheck-repl itself. That rule could simply check to see if 'var' exists in the current shell's environment or if it's defined in the user's current command.

I think bash is simple enough that you might not have to a full-blown parse on the input to pick out instances of variable use (just look for 'export var', 'unset var', 'var=', and the like). You'd also want to take into account special variables like $RANDOM and $HOSTNAME, but that's pretty trivial.

[+] nerdyadventurer|4 years ago|reply
I am not an expert, so forgive me for asking, Does Zsh also has this pitfalls?
[+] asicsp|4 years ago|reply
[+] unfocussed_mike|4 years ago|reply
The title of this article brings to mind an imaginary platform game with controls so complex you have to have three browser tabs of instructions open to play it.

I am hopeful that the page will have a screenshot, when I am able to access it.

[+] rr808|4 years ago|reply
bash is a pitfall. In this world of IAC, powerful tools, unit testing, CICD, well crafted languages and libraries its a relic of an old time long gone.
[+] skrtskrt|4 years ago|reply
there's just no way we should all be scripting in a language where typing out anything more than a single command necessitates a cascade of "well ACKshually it should be done this way" corrections, whether it's from a static analysis tools or your grey-bearded Bash-wizard coworkers.

We are eventually going to look at using bash and similar shells for scripting as the bad old days.

[+] deknos|4 years ago|reply
and still it's the only one which is easily available everywhere and for the most part already in the allow/whitelist of tooling in most organizations as redhat and debian/ubuntu are almost everywhere.
[+] dehrmann|4 years ago|reply
But practically everything unixy supports some shell variant that's similar to the others, so we have no choice in the matter. I compare this to PHP. They're both bad, PHP is a choice.
[+] chomp|4 years ago|reply
Can’t load the link, but I’m intimately familiar with Greg’s wiki. It’s been around for ages and a very great resource for Bash arcana.
[+] joshbaptiste|4 years ago|reply
This and https://wiki.bash-hackers.org, #bash on irc (libera now) helped me tremendously on writing hardened scripts in production.
[+] cybarDOTlive|4 years ago|reply
nice thanks for sharing!

https://github.com/dylanaraps/pure-bash-bible

>The goal of this book is to document commonly-known and lesser-known methods of doing various tasks using only built-in bash features. Using the snippets from this bible can help remove unneeded dependencies from scripts and in most cases make them faster.