Tag Archives: bash

Bash #4 – Quote function parameters

I try to extract stuff into functions both for re-use and encapsulation. Coming from Java Bash itself leaves a lot to be desired in this area but that´s a different post. What I´ve found is that if I don´t encapsulate input variables with quotes it can become quite confusing what is defined and not. If you don´t quote and a variable is unset, it will shift your input.

function myfunc() {
	local OPTION_ONE=$1
	local OPTION_TWO=$2

	echo "$OPTION_ONE and $OPTION_TWO"
}

VARTWO="hello"

myfunc $VARONE $VARTWO
myfunc "$VARONE" "$VARTWO"

In the first (unquoted) line above OPTION_ONE inside the functio will “become” VARTWO. In the second example with quotes it´s easier to understand that VARONE is the one missing, because it doesn´t shift it to the left.

Bash #2

Part of what I find really hard with Bash scripts is encapsulation and error handling. It all became a tiny bit better when I discovered how I can print the call stack when something occurs. Check out this blog post for how to print the call stack.

I’m thinking this fits nicely with trap, but I’ll have to try that another day. 🙂

Bash #1

I’m currently working on automating our deployment with Bash scripts. For a TDD loving Java programmer it can be quite hard to figure this all out, but I’m getting better. I don’t ever think I’ll start to love it, but maybe one day I can like it. 😉 I’ll try to share small snippets of what I learn here.


set -e
set -u

I always have these early in my scripts. That way I know if anything fails (set -e) and if any variables is undefined (set -u).

Any other tips?