Take a look at this simple example:
x="foo bar"
echo ${!x}
It erros: -bash: foo bar: invalid variable name.
I'm writing a script where a positional argument will indicate a "color name" and we use indirect references to fetch the hex code of the color name. I want the script to keep going if the user passes a color name with space in it or generally an invalid color name (which we will default to white).
I also tried this try/catch approach but to no avail:
x="foo bar"
echo ${!x} || echo error
The closes thing I could find was this, but didn't help:
bash, how about switching to an array?declarefor that, e.g.declare -- "${x}" 2>/dev/null || echo errordeclare -pinstead ofdeclare ---- otherwise too much chance of that user input being something that has side effects when run.x='foo bar'while the description mentions processing colors; recommend you rewrite the question to focus on your actual (color) issue: a) show the setup of a couple colors, b) show your code working for a valid color and c) show your code's result with an invalid color (ie, show all stdout/stderr output); net result: provide a minimal reproducible example