Is there any easier/more elegant way to assign a return value to a variable? So far I've been doing something like:
some_command_to_run
VAR_TO_SET=$?
But I would much prefer to have it all on one line and preferably without using the $? variable. Does anyone know how?
The question I have is "Why?". It's the facility provided by Bash to accomplish that function.
However, you can do this (in addition to what Kyle and Gilles showed):
That's all there is. If you really want it on one line, you can write
some_command_to_run; VAR_TO_SET=$?
. You could make it a two-liner function, but I don't recommend it because it will make your scripts less readable (one more thing to learn to be able to understand your code).That is all I know as well. However, if you are just doing this repeatily to see if I command was sucessful with something like:
You can actually just test a command directory like:
You might have already know that, but figured it might help you :-)