As usual there is more then one way to do what you were trying to.
This is a (maybe compact) way to do it:
#!/bin/bash
TAR_COMMAND='tar -cf Archiv.tar myfolder/ myotherfolder/ 2>/dev/null'
MSG_OK='### Work well DONE ### ' # A more common way to use a variable
MSG_ERR='### OPS IT FAILS ### '
$TAR_COMMAND && echo $MSG_OK || echo $MSG_ERR
Short notes
If you have no need to store the exit status (or the exit code) of your command you can use immediately after the operators AND,
&&, and OR,||[11].
The logic:cmd1 && cmd2,cmd2will executed only ifcmd1exits without errors (with exit code0). Instead withcmd1 || cmd3,cmd3will be executed only ifcmd1will exits with an error (error code different from0).If, instead, you want to store the exit code of a command you have to do it immediately after its execution, because with the next command execution this value will be updated. It's enough a variable assignation
mycommand # you execute your command ExitCodeToUseLater=$? # So I can use it later in the script # ... other stuffs ... [ $ExitCodeToUseLater = 0 ] && echo "It was gone ok " # ... again other stuffs ... [ $ExitCodeToUseLater = 0 ] || echo "# Never a time that it goes as I want"In bash scripting, as well as in the makefiles [2a,2b], you can find a command with many options written in a variable in order to make more readable the code (as you can see above for the
TAR_COMMANDvariable). Writing in a shell the variable$MyVarit will be the same to write in the shell what is written inside [3]. It works in a script too. You may have to pay attention to the bash expansion and substitution rules about which you can read more fromman bash./dev/nullis the Null device [4], "a device file that discards all data written to it but reports that the write operation succeeded". It can be used to give an empty input to a command (</dev/null) or to pass in "absolute silence" the standard output or the standard error [5].