If you are sure that the status will be only one line, you can do something similar with sed:
status=$(echo "$status" | sed -e 's:^foo$:bar:' -e 's:^baz$:buh:')
You can also get something to work with bash's built-in replacement. This almost works (I don't know how to get an exact match):
status=${status/foo/bar} status=${status/baz/buh}
If your goal is to be more “functional” (and so that your code is no longer sealed), you can do this:
status=$( case "$status" in ("foo") echo "bar" ;; ("baz") echo "buh" ;; (*) echo "$status" ;; esac)
Although, frankly, bash is probably one of the worst languages to try and be functional. It was truly designed with more imperative thinking, as evidenced by the fact that you cannot easily compose expressions. Look in the second code snippet, how did I have to break it into two separate statements? If bash were designed to work, you could write something like this:
status=${{status/baz/buh}/foo/bar}
But this does not work.
I suggest using only bash for simpler scripts, and for more complex things, use something like Python or Ruby. They will allow you to write more functional code without having to constantly struggle with the language.
Laurence gonsalves
source share