Skip to Content
Navigation:

A stick figure smiling

Here's a preview from my zine, Bite Size Bash!! If you want to see more comics like this, sign up for my saturday comics newsletter or browse more comics!

Image of a comic. To read the full HTML alt text, click "read the transcript".

browse more comics! get the zine!
read the transcript!

for loop syntax

for i in panda swan
do
   echo "$i"
done

the semicolons are weird

usually in bash you can always replace a newline with a semicolon. But not with for loops!

for i in a b; do ...; done

you need semicolons before do and done but it’s a syntax error to put one after do

looping over files is easy

for i in *.png
do
   convert "$i" "${i/png/jpg}" 
done

this converts all png files to jpgs!

for loops loop over words, not lines

for word in $(cat file.txt)

loops over every word in the file, NOT every line (see page 18 for how to change this!)

while loop syntax

while COMMAND
do
   ...
done

like an if statement, runs COMMAND and checks if it returns 0 (success)

how to loop over a range of numbers

3 ways:

for i in $(seq 1 5) 
for i in {1..5} 
for ((i=1; i<6; i++)

the second two only work in bash, not sh

Saturday Morning Comics!

Want another comic like this in your email every Saturday? Sign up here!

I'll send you one of my favourite comics from my archives every Saturday.
© Julia Evans 2024 | All rights reserved (see the FAQ for notes about licensing)