Learning Shell Scripting
I’ve been learning Shell scripting for a while by following the Linux Command Line and Shell Scripting Bible one chapter at a time. My approach is fairly slow, because I read a chapter and then try to write a small program that uses whatever command or trick it just introduced, since I am a practical learner.
My practice scripts all live in a folder called bible, one file per chapter, and I just keep them to look back and see where I once was. This was one of my earlier programs:
if [ -f "$file" ]; then
echo "$file is a file"
elif [ -d "$file" ]; then
echo "$file is a directory"
else
echo "wtf even is $file?"
fi
Learning Shell makes me appreciate features I take for granted in modern languages. Even simple decimal math requires very precise syntax and a call to the bc command. String handling can feel even more finicky because a wrong quote and it is interpreted as a different datatype.
Scripts I use
I originally wanted to learn Shell because I work with CLIs frequently and wanted to cut useless actions by writing scripts to automate them. Here’s one of them called mf to make a sequence of test files with similar names.
for ((i=1; i<=n; i++))
do
touch "$basename$i.txt"
done
A couple of others do similarly modest jobs, like xlist, which lists the executable files in a folder, and read_env, which pulls variables out of a .env file. On their own they are simple but when I repeat a command a hundred times everyday, a script can feel much quicker and robust.
init
I’m still in the Linux Distro-hopping phase, so I switch OS often and don’t want to deal with setting up a system every time. I made a script called init to set up a fresh machine for me. It makes my other scripts executable, installs the packages I use, updates the system, clones my dotfiles, and stows them into place, though it is still full of TODO features.
Shell scripting is a useful language to learn because it is useful no matter what your tech stack is. And it gives you a new perspective on programming as a student. I feel like this skill will stick with me for the rest of my career.