I'm writing a bash script that is supposed to be "transparent" to the user. It reads commands from the user and intercepts them, allowing only some of them to be executed by bash, depending on some criteria. It (basically) works like this:
while true; do
   read COMMAND
   can_be_done $COMMAND
   if [ $? == 0 ]; then
      eval $COMMAND
      if [ $? != 0 ]; then
         echo "Error: command not found"
      fi
   fi
done
The problem is, when the command fails, you also get stuff printed to the console. BUT, if I keep the result in a variable and only print it when it doesn't fail, like so:
RESULT=$(eval $COMMAND)
Then there's another problem: The special formatting gets lost (for example, "ls --color" doesn't show colors anymore)
My question is: Is there a way to have the command print to STDOUT if successful, but to /dev/null if it fails?


ls --colormeansls --color=always. Usuallylsis aliased tols --color=autoorls --color=tty, i.e. it only prints color codes when stdout is a terminal (isatty).alias ls='ls --color'then you'll have problems usinglsin a pipeline (when stdout is not a tty).