haltack
haltack

Reputation: 3

Shell one-liner: How to get the command executed before "&&"?

I'd like to do the thing below:

  1. execute a command, commandA
  2. if commandA was executed successfully, get commandA as a string
  3. notify that commandA was executed successfully

To achieve step 2, what commands should be used?

commandA && lastcommand=<some skillful commands> && echo '$lastcommand was executed successfully!'

Upvotes: 0

Views: 95

Answers (1)

Barmar
Barmar

Reputation: 781210

bash doesn't have anything that does this automatically.

Instead of trying to get the command, put it into a variable and execute that variable.

lastcommand=(commandA arg1 arg2 arg3) && "${lastcommand[@]}" && echo "${lastcommand[@]} was executed successfully"

Upvotes: 2

Related Questions