r/devops • • Dec 04 '22

Searching for a good bash course to improve my scripting skills

It can be a book or video course it whatever. My only requirement is to be a complete guide ( not post for solving a specific problem).

Thanks.

70 Upvotes

25 comments sorted by

27

u/xiongchiamiov Site Reliability Engineer Dec 04 '22

http://mywiki.wooledge.org/BashPitfalls is not a guide per se, but it's important reading for anyone doing shell scripting. Also read the separate page on Quotes.

1

u/dereksalerno Dec 05 '22

I just scanned over that list and good god, I hope OP checks it out. I thought I was pretty great at bash but have been using it a whole lot more than I used to in my current role (the last 6 months). This list has so many things that have brought me unnecessary pain recently.

3

u/xiongchiamiov Site Reliability Engineer Dec 05 '22

If you install shellcheck, that gives you a linter that's basically a programmatic version of that list. It helps me catch stuff I do all the time.

Also, http://redsymbol.net/articles/unofficial-bash-strict-mode/ (at least the non-IFS part) goes in every script I start writing and that flushes a lot of bugs.

1

u/dereksalerno Dec 05 '22

Thank you! I actually switched to neovim and have been using the bash-language-server lsp plugin and neovim/shellcheck.nvim. I got my confidence back and feel like a competent basher again — I just wanted to nightlight how cool/triggering it was to see all of this gathered in one place!

Edit: my phone autocorrected LSP to lap

23

u/nullpackets Dec 04 '22

Installing shell check really helped me pick up the finer details mistakes I kept making, it's a linter for bash and helps you grow out of old habits with contextual help

https://www.shellcheck.net/

7

u/humoroushaxor Dec 04 '22

Also has a vscode plugin with live highlighting.

3

u/d47 Dec 04 '22

Yep, shell check should be mandatory

10

u/Thwonp Dec 04 '22

exercism.org has a good bash track

4

u/gregnorz Dec 04 '22

exercism is great for everything I’ve tried to learn there. I have a few “strong” languages in my portfolio, and this site has been great for a quick learn of other languages to see what they’re like.

You can combine with https://repl.it if you don’t like their interface. Sometimes I prefer to find a proper IDE and Docker setup (if possible) for the language so I can get a developer-eye view of the environment.

3

u/type1advocate Dec 04 '22

You can do full git pulls of Exercism lessons and push your answers, so you can use whatever IDE you want.

2

u/gregnorz Dec 04 '22

Yeah, good stuff. Very valuable for a free service, I’d say!

9

u/futurecomputer3000 Dec 04 '22

Best thing is just to start imo. Do something cool with your machine.

5

u/ManFrontSinger Dec 04 '22

The Linux Command Line - No Starch Press

I forget the author. You can do the googling.

4

u/Romanmir Dec 04 '22

This is usually my go-to for bash scripting: https://tldp.org/LDP/abs/html/index.html

1

u/biffbobfred Dec 05 '22

Was gonna post this. Yeah. Lots of good example code here

3

u/Atik Dec 04 '22

There's also a video course from Learn Linux TV: Bash Scripting on Linux

2

u/samrocketman Dec 04 '22

I highly recommend reading the bash man page. You can convert it to PDF using a simple command like this man2pdf.

Some examples of things you'll learn:

  • Functions take compound commands. People treat bash like other programming languages but it is more powerful at specific things. Functions can take more than {} which is unlike other programming languages.
  • test or [ is actually a program (which [) and also a shell builtin. Because it is a program, it takes space separated arguments. Coming from other programming languages people treat if [ ... ]; then the same as if(...) { but once you understand it is a program it becomes easier to remember the syntax.
  • Use bash arrays to provide arguments to commands inside of the script instead of using something like eval. Keep the arrays simple and do not use multidimensional arrays for the most compatibility.

I plan to write a blog post but I've been kind of sitting on it for a few years.

2

u/izalac Dec 05 '22

As someone who is self-taught, if you want to learn it fast, the best advice I could give you is - dive into it fully. If you're currently not running Linux, run it on real hardware - as a dual boot, on an old laptop, whatever, and use it as your main computer for as much as you can. Do your workflows in it. Use command line tools as much as possible.

Experiment with standard linux commands, pipes, input, output - that's bread and butter of bash. Note some parallels, e.g. between vim commands and sed parameters. Learn at least some basics of awk so you can use it in your pipelines. Play with cron, tar, openssh, systemd, and mail transfer agents such as exim and postfix. Reformat some logs.

The knowledge of what you can do with Linux's command line tools and how to string them together is the most important part of being effective in bash. You get that knowledge far quicker when you're fully immersed in the OS running it. Maybe try installing Arch Linux (on real hardware or VM) in the classic, wiki way as a practice to get more comfortable in shell.

Knowing some other scripting solutions, such as Python or Ansible will also help you determine when to use bash, and when to use something else.

2

u/whetu Dec 04 '22

With all due respect given to this sub, /r/bash is the better place to look/ask. This thread from 6 days ago is relevant to your interests

-2

u/Petelah Dec 04 '22

Anytime you want to write something generic in some programming language write it in a bash script.