Alex Rivera | Logout

Case statements evaluate to strings

Asked 2010-02-17T19:12:46.933
23

I've caught the functional programming bug, so naturally nothing is good enough for me anymore. ;)

So, in bash one could write:

case $status in
  "foo") status="bar" ;;
  "baz") status="buh" ;;
   *) status=$status ;;
esac

but I'm afraid of typos, so I'd prefer to write:

status=case $status in
  "foo") "bar" ;;
  "baz") "buh" ;;
  *) $status ;;
esac

The second form is invalid since the case evaluates to the exit code of the last executed command, which is not at all what I'm looking for.

Are there easy hacks to achieving what I am looking for?

Edit
Report

1 Answer

6
status="baz"
status=$(case $status in
  "foo") echo "bar" ;;
  "baz") echo "buh" ;;
  *) echo $status ;;
esac)
echo "status: $status"

output

$ ./shell.sh
status: buh
answered 2010-02-18T00:43:49.180

Your Answer