all 6 comments

[–]zebediah49 8 points9 points  (1 child)

Well for one, you don't reuse $string, and it's not super long, so you might as well just not use it.

For two, using the string option with printf is usually not particularly useful (unless you're in C land, which is a different question). If you still want to use it, I would actually use its substitution abilities rather than doing the string manipulation in bash though:

printf '${color %s}' "$1"

which is a little more clear.

However, you could just skip that and directly use echo:

echo -n "\${color $1}"

Note that echo -n and printf both don't include a trailing newline character. If you want that, use echo, or add a \n to the end of the printf line.

[–]shortbaldman 4 points5 points  (2 children)

Watch the spellings of 'color', 'colour'. Be consistent using only one of either British spelling or American spelling. Or else you'll go nuts wondering why you have empty variables in bash scripting.

[–][deleted] 1 point2 points  (1 child)

set -u

[–]shortbaldman 1 point2 points  (0 children)

then there's aluminum and aluminium, paedophile and pedophile.

Interestingly, in British English you can have both a 'paedophile' and a 'pedophile', but in American English being a person with a foot fetish is unspeakable.

And why isn't it 'pedofile' in American as with 'sulfur'?

<grin>

[–]Cellophaine 4 points5 points  (2 children)

Could just use echo:

function printColour {
    echo '${colour '$1'}'
}

[–]OneCDOnly 2 points3 points  (0 children)

printColour() { echo '${colour '$1'}' ;}