Saturday, March 01, 2003

CodeBit; Using Hashes in Bash Scripts

This codebit was donated by Phil Howard on 1998Dec03. Hash data structures are very convenient. They let you use strings as indexes instead of numbers. The value script, shown below, demonstrates how to use Bash's eval command to simulate a hash structure. The command, value david prints "The email address for david is medined@mtolive.com".

#!/bin/bash
# value.sh

email_phil="phil@rigel.ipal.net"
email_david="medined@mtolive.com"

eval 'email=${email_'"${1}"'}'
echo "The email address for ${1} is ${email}"
Post a Comment