July 17th, 2024

Free Introduction to Bash Scripting eBook

The GitHub guide on Bash scripting covers Bash structure, variables, loops, functions, and debugging. It offers downloads in dark mode, light mode, and ePub format. Additional resources include sponsors, a web page, video course, training, author details, PDF generation tool, and book cover creation tool. Links to the author's blog and other ebooks are provided for further exploration.

Read original articleLink Icon
Free Introduction to Bash Scripting eBook

The GitHub guide on Bash scripting provides comprehensive coverage of topics such as Bash structure, variables, loops, functions, and debugging. It offers downloads for the ebook in dark mode, light mode, and ePub format. The guide includes details about sponsors, a web page displaying the ebook, a mini video course, interactive training, author information, the PDF generation tool, book cover creation tool, and links to the author's blog and other ebooks. For further exploration or access to specific content, feel free to specify your preferences.

Link Icon 22 comments
By @tjoff - 3 months
A lot of people suggests python, I'd argue that python is a bad fit for what many bash scripts does.

A bash script is basically scripting what you do on your terminal. I feel like scripting in the same language that is your terminal interface is underrated.

You can basically take a look at your command history and wrap it up as a script.

Yes, bash is clunky as hell. It has some really ugly warts. But learning the basics will make you more proficient in the terminal. The synergies are really nice.

By @Too - 3 months
Just skimmed through the book and noticed several examples where it doesn't use argument quoting. For example the first chapter about variables doing echo $name. In fact, there is no mention of argument quoting at all. This should be the number one lesson before touching any bash script. Otherwise your scripts will be vulnerable to command injection. I recommend running the whole book through shellcheck.

It also recommends wrapping variables with curly braces like ${name}, without explaining why, later on never does so itself throughout the rest of the book. There are a lot of such missing explanations in the whole book. It does make it short and fun to jump straight into the examples but i think it could benefit from some facts and reference material, at least to link to.

Anyway, seems like most people here are more interested in arguing over bash vs python than discussing the actual content. A little surprising to see this get so many points.

By @qz_kb - 3 months
Using python and constraining yourself to only use a basic subset of the standard library modules so you can run the script in pretty much any environment is almost always a better choice than trying to write even one loop, if statement, or argument parser in a bash script.

bash script is "okay" I guess if your "script" is just a series of commands with no control flow.

By @xlii - 3 months
Recently I’ve been exploring back Perl. I loved it many years ago and today I just need “glue”.

It seems to be quite fast, can escape UNIX-Linux idiosyncrasies and I still can see all the magic variables in the code after those years.

Maybe it’s better to just get back into Perl instead of figuring out how to pipe back to back to few applications and jq on top?

By @baxuz - 3 months
Tried https://github.com/google/zx and never looked back.
By @asicsp - 3 months
By @alwinaugustin - 3 months
I am new to bash and always struck at using the tools like `awk` and `sed.` You need to be good at regular expressions to write a good script using these tools.
By @jimmar - 3 months
I've coded in a bunch of languages over my career, but somehow managed not to use bash. But a few weeks ago I found myself needing to create a somewhat involved script using Bash. Using GitHub Copilot, I was able to generate a script that worked great and it included comments, arrays, arguments, case statements, loops, conditionals, menus, etc--basically covering the first dozen chapters of this book. I guess my point is that we need to teach people what's possible and to help them explain what they want clearly. Memorizing syntax will be a less relevant skill at some point.
By @pronoiac - 3 months
I recently was running a variety of compressors over the course of days, and using Bash was great because, it doesn't parse the whole file before it starts running - I could edit and reprioritize next steps while the script was running. "Oh, that one's not working, skip the slower version" or "oh, space is an issue, work on freeing that up." It's a weird hack I wouldn't suggest in production, but it worked well for me.
By @ayakang31415 - 3 months
Is there anything Bash does that Python can't do? I feel like using Python is just way better than using Bash.
By @udev4096 - 3 months
If anyone's looking to write tests for bash scripts, I wrote a small blog post few weeks ago: https://hcrypt.net/test.html
By @bobbyiliev - 3 months
Many people are saying that ChatGPT or Copilot could write great scripts which is indeed true, but the eBook was written way before those LLMs were a thing. It is still good to learn the basics though.
By @callalex - 3 months
Can someone name me examples of where a bash script is actually the _correct_ tool for the job in modern times? Or is it still used only because it’s the easily available tool?
By @TheLoafOfBread - 3 months
Or you can use PowerShell - Available on MacOS, Windows, Linux instead of suffering through incoherent syntax of Bash.
By @ngcc_hk - 3 months
Bash scripting is hard ... missing a space or add a space ...

How do you debug it and trace it ...

I do use it as said this is the terminal command line and both Mac/Linux supported it. And if it works, it work even nohup.

Just hard.

By @macrael - 3 months
Write scripts in whatever tooling you have setup for your app. In a typescript codebase, I write typescript scripts. In a go codebase I write go scripts. It's so easy to write a couple wrappers to make execing something a one liner and then the advantages of bash have disappeared. You already have a toolchain for running a program in your language, just use that to build your scripts.
By @1vuio0pswjnm7 - 3 months
Bash is not a good shell for scripting. Too big and slow.
By @foreigner - 3 months
Debating the pros and cons of different languages misses the point IMO. Shell scripts are useful not because of the language but because the interpreter is already available absolutely everywhere.
By @jofer - 3 months
For anyone wondering whether or not you really need to know bash: Yes. Yes, you do.

Somewhere in the middle of every stack is a bunch of bash and makefiles. Don't replace that or avoid it. Embrace it. It's not perfect, but it's important to know.

By @udev4096 - 3 months
This looks extremely basic. I would instead recommend reading https://bash.academy or TLDP Advance Bash Scripting Guide
By @rty32 - 3 months
I know basic bash and can read most bash scripts, but never spent much time on it to become an expert. However I found ChatGPT to be extremely valuable -- given the description of a task, it can create very high quality scripts with best practice and comments, often correctly using third party tools. That's probably what I'll do going forward -- my time is better spent creating a good prompt and reviewing output.