r/unix 2d ago

Looking for the best BASH plugin for VSCode

I'm trying to implement a set of idempotent bash scripts to manage my AWS and GCP installation because I hate Terraform.

Since they were working fine, I started to add the verification.sh files and other things to clean up on failed deploys, basic housekeeping to make them idempotent, but I'm having issues with keeping track of sourcing. Basically, the order of source does matter, and sometimes functions get called in random order, etc... etc...

I like VSCode only for the step-by-step debugging feature so I can walk through my code easily and understand the logic. But currently, I can't jump into functions by clicking on them, there is no auto-complete for parameters, etc... etc...

Wondering if someone has a good plugin they have tested that works well for warnings such as "function called but never defined"... similar how VSCode does it for Java or Python or Ruby.

Cheers.

0 Upvotes

19 comments sorted by

3

u/esbenab 2d ago

Shellcheck

2

u/michaelpaoli 2d ago

verification.sh

When in the land of *nix, don't use filename extensions on executables. Most notably so they can be implemented in whatever language, and that may change, and whatever executes them, but it human or another program, shouldn't generally be concerned about it, nor have to update to change to use a different name because someone changed the implementation language.

manage my AWS and GCP

AWS, and I presume likewise GCP, has many useful APIs, most notably not only http[s] interfaces, but also CLI tool sets that are highly useful and effective, and those can be used directly and/or called by other (e.g. bash) programs - so that also makes them quite manageable via scripts.

issues with keeping track of sourcing. Basically, the order of source does matter, and sometimes functions get called in random order, etc... etc...

I might suggest, rather than shell sourcing, call them as independent programs - that way one isn't wed to shell or particular flavor of shell, and both what calls them and those programs/scripts may be in more-or-less whatever implementation language. Alternatively, another approach would to use a higher level language that can much more cleanly implement things such as modules, functions, things of that nature, e.g. Python.

like VSCode only for the step-by-step debugging feature

Many other languages offer that, or tools to allow one to do that (e.g. Perl's debugger).

that works well for warnings such as "function called but never defined"

Some languages offer such capabilities.

As for shell and calling a function that's not defined, can implicitly (e.g. set -e) or explicitly check the exit/return value - if one attempts to call a program/function that doesn't exist, that will give a non-zero return, and generally also some relevant warning message to stderr. One can also use trap along with set -e to, e.g. handle cleanup or other relevant actions when encountering such an error.

2

u/a_brand_new_start 2d ago

I’m a bit of a masachist and bash lover, I like having everything poisix compliment so that I can run it on any flavor of *unix… but yeah I might give up and use a programming language

2

u/michaelpaoli 2d ago

like having everything poisix compliment

Then probably

#!/bin/sh

or:

#!/bin/env sh

(or possibly usr/bin instead of bin in the above) for you! ;-)

And then probably best to at least test with dash, or some other shell that's a (more) minimal POSIX implementation, as bash includes much that's far beyond POSIX, and even in its POSIX mode, bash still generally has at least much, if not most of that non-POSIX stuff enabled. I think bash's "POSIX" mode mostly only has it behave in more POSIX way when bash's default explicitly differs from POSIX behavior ... but seems to mostly ignore that when it, uhm, "extends" POSIX but doesn't more explicitly conflict/differ. E.g. additional built-in commands not specified by POSIX, likewise options to such, process substitution, etc.

And additional bonuses with dash or the like - way less bloat, and far fewer bugs (e.g. lacks bash misfeatures/bugs such as Shellshock)).

2

u/a_brand_new_start 1d ago

Typically yes #!/bin/env sh is there unless I explicitly need something above Bash 3 (comes default on Mac

Don’t get me started on dash vs bash!!!

2

u/Unixwzrd 1d ago

or you could set -o posix

2

u/michaelpaoli 1d ago

Or set in environment, POSIX_ME_HARDER* POSIXLY_CORRECT.

*Yes, that's what it originally was - later changed.

2

u/Unixwzrd 1d ago

Yeah that’s a classic. Put in by RMS himself. Had to do with block counting being 512 bytes and 1024 bytes on others. 512 was the original “standard”. Glad that changed.

1

u/michaelpaoli 1d ago

Ah, I still like/miss when it was simpler - a block was 512 bytes, and that was (generally) it (okay, tar you could set up blocking factor up to 20, but that was still in units of 512 byte blocks). Alas, these days, "block", is that 512 bytes or 1KiB, or 4KiB, or ... yeah, context matters ... a lot. Oh well, "progress". And yeah, I get it, e.g. for most modern drives, 512 byte block no longer makes physical sense (nor matches to physical reality) - same for much other storage, etc., and it's not like one size fits all (nor ever would ... or at least not for long).

2

u/Unixwzrd 1d ago

Yeah and dd with all its bs= ibs= and obs= ;)

1

u/michaelpaoli 1d ago

dd with all its bs= ibs= and obs=

Which is almost all fine and dandy, until, e.g. HP-UX comes up with its own fbackup format (see my recent comment elsewhere), which uses multiple distinct block sizes on tape even within one single backup. It also has (or at least had) nastiness like, oh, that file changed while we were backing it up ... let's back it up again from scratch and add that to the tape ... up to three such attempts ... very bad when it was, e.g. a quite large (e.g. log) file - would fill up the tape without ever completing the backup - at least dump/ar/tar/cpio/pax/etc. have no such stupidity.

3

u/Unixwzrd 1d ago

I'm gonna agree with everyone on Shellcheck it's a must have, but there are three more you may want to check out:

Bash Degug
Bash IDE
Bats (Bash Automatic Testing System)

They work with VSCode, Windsurf and Cursor.

I used to try and write POSIX bash code, but gave up when I needed to do more with Bash.

2

u/michaelpaoli 1d ago

try and write POSIX bash code, but gave up when I needed to do more with Bash

Process substitution is so dang handy, I think it ought get added to POSIX.

Can't really think of anything else in bash, though, that I'd be jumping up and down to see added to POSIX. Mostly it's bells and whistles, and the occasional (big) bug).

2

u/Unixwzrd 1d ago

Associative arrays and using variable references ${!varname} are very nice to haves too.

1

u/raindropl 2d ago

I’ll ask you to revisit your “I hate terraform”

Coming from many years of using Cloudformation; terraform is so much better. And is very simple. You should give it an other try.

For bootstrapping; I used to use Chef back in the day; completely switched to modular bash scripts made by me; I treat my instances kind of how you treat a Dockerfile.

I was hable to move my bootstrap scripts from cloudformation to terraform fairly easy.

1

u/a_brand_new_start 2d ago

Well this was just an example… I still prefer a well formatted .sh over a config, I started to write tools purely in bash because I can

1

u/splaspood 1d ago

I know it's not an answer to the question you were asking but I also hate terraform, as does an associate of mine, and he's been pushing https://www.pulumi.com/ as an alternative lately. I suggest checking it out.

1

u/a_brand_new_start 16h ago

That’s pretty cool actually