Skip to Content
Navigation:

A stick figure smiling

If you want to see more comics like this, sign up for my saturday comics newsletter or browse more comics!

browse more comics!
read the transcript!

panel 1: defining functions is easy

say_hello() {
    echo "hello!"
}

and so is calling them:

say_hello

(no parentheses when calling a function!

panel 2: functions have exit codes

failing_function () {
    return 1
}

0 is a success, everything else is a failure. A program’s exit codes work the same way – 0 is success, everything else is failure.

panel 3: you can’t return a string

you can only return an exit code from 0 to 255

panel 4: arguments are $1, $2, $3, etc

say_hello() {
    echo "Hello, $1!"
}
say_hello "Ahmed"

the above code prints Hello, Ahmed!. Again, say_hello "Ahmed", not say_hello("Ahmed")

panel 5: The local keyword declares local variables

say_hello() {
    local x
    x=$(date) # this is a local variable
    y=$(date) # this is a global variable
}

panel 6: local x=VALUE suppresses errors

this line never fails, even if asdf doesn’t exist:

local x=$(asdf)

but this will fail (as you would expect) – if you have set -e set, it’ll stop the program

local x
x=$(asdf) # this line will fail

person: “I really have NO IDEA why it’s like this, bash is weird sometimes”

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 2020 | All rights reserved (see licensing)