How to Git Gud with Shell: tips, ideas, and resources

Without question, getting good with the shell is the most life-changing thing you can do if you’re a linux user. Read this post for tips on getting good, ideas for projects to accomplish with shell scripts, and resources for learning more.

by spitemim, 2022-08-14


Without question, shell (and shell script) is the most useful language to learn if you’re running Linux. Punching in a few lines at the shell prompt can save you hours of tedious, repetitive, manual work if you know what you’re doing. Even if you spend more time automating something than it would take to actually do the thing, writing a script to do something repetitive is more interesting than actually doing it. The more you practice with it, the more knowledge you gain that can be applied to other projects.

Getting good with the shell, just like getting good with any other computer language, can take a while especially if you’re not coding-oriented. In this post, I want to share some tips that I’ve picked up throughout my travels on the internet, as well as video and text-based resources that helped me progress, and project ideas for writing shell scripts.

(If you just want to skip my rants and get to the good stuff, click here.)

Shell vs. Bash/zsh

It’s important to make this distinction right off the bat. When I say shell, I’m talking about sh. Not bash, not zsh, ESPECIALLY not fish, just sh.

sh is a specification for how a shell should act, created by POSIX. It’s essentially a set of rules and requirements that so-called POSIX compliant shells should follow. Since it’s a specification and not an implementation, /bin/sh is often a symbolic link to an implementation of sh such as dash.

NOTE: On Arch and many other systems, sh is just a symlink to bash, which will try its best to be POSIX-compliant when invoked as sh. Bash is significantly slower than other sh implementations, like dash for example, so you might wanna install another shell like dash and symlink it to /bin/sh. Like this (run as root): ln -sf "$(which dash)" /bin/sh

Bash and zsh support most of the features of the POSIX shell, but they also have their own features, known as bash-isms or zsh-isms. All these extra features cause bash and zsh to be slower and more bloated than plain sh implementations. Sometimes, POSIX sh scripts will behave differently when running in these shells due to the extended featuresets. I still use zsh as my main shell – the one I use when I’m just sitting at my terminal not scripting, because a lot of those extra features provide niceties like smart tab-completion. I don’t use zsh-isms when I’m just sitting at my terminal because I’m just not interested in learning features that I’m not going to use in scripts.

sh is a much simpler language, more portable across *NIX systems, and common implementations of it are faster too. If you’re writing software complex enough that you need the extra features of bash or zsh, you might be better off writing it in an actual language – see Python, Perl, Lua, C, Awk for that. This SO answer sums up my thoughts on sh pretty well, if you want a tl;dr.

That being said, bash and zsh are still powerful shell languges that can do a lot of stuff faster and easier than languages like Python. If you wanna write bash scripts, I’m not gonna fault you.

Shell vs. Shell Scripting

When I say you should get good with the shell, I mean getting good with the interactive shell AND with shell scripts. It might make sense to do most of your initial learning just by punching stuff into the terminal, since that just requires the core shell knowledge – how to use commands, how to use slightly more advanced syntax like subshells and stuff, and so on. Shell scripting is the art of taking that knowledge and wrapping it with extra logic to be used as a standalone program. A good UNIX tool should handle commandline args (if applicable to the problem at hand,) report errors, and generally not break down due to unexpected input. Taking one or more shell one-liners that accomplish a task, putting them together, and making the whole thing robust enough for use in other programs is the art of shell scripting. a For example, you could learn how to combine a still image and audio file into a video file with ffmpeg. That’s just shell knowledge. Shell scripting is figuring out how to put that into a script with extra logic for convenience, maybe it lets you pass image and audio filenames as arguments, does sanity checks like making sure the files exist, reports errors/usage statements, and whatever else.

TIPS/RESOURCES DUMP

Stuff to learn

People to watch

Bugswriter and Luke Smith are pretty much the only youtubers worth watching when it comes to shell script that I know of.

Bugswriter is probably hands-down the greatest youtuber you could possibly watch if you wanna learn the shell. His older thumbnails are cringe, but don’t let that fool you. His videos were super instructive when I was first learning shell scripting over a year ago, and I made a lot of really cool scripts that I still use today.

This is the number one thing you should take from this post: WATCH BUGSWRITER’S VIDEOS. If you listen to nothing else on this page, at least listen to this.

Luke smith doesn’t post much shell scripting related content anymore. His older videos on shell scripting and how to use basic commands are really useful for beginners though. He also has videos on regular expressions and stuff like that.

Stuff to Read

Read the dash manpageman dash. You will learn some useful and interesting stuff about shell syntax that no one on Youtube will tell you because no one else is enough of a depraved nerd to read the dash manpage.

Ideas for scripts