bash: string to integer conversion

Bash arrays are odd, globally scoped critters which can only be indexed by integer values… but the values don’t have to be contiguous so they are sort of like hash maps…

All you need is something to convert strings to integers, for example:

_map_string_to_integer() {
	echo ${*} | od -t u1 | sed 's,[^ ]*,,;s, ,,g;' | tr -d '\n'
	echo
}

And you can use it like this:

% fun[$(_map_string_to_integer this)]="is neat"
% echo ${fun[$(_map_string_to_integer this)]}
is neat

if that is of any use to anyone….

Leave a Comment