I know that $? gives the status of the previous command but how can I get status of a particular command.

rsync -avh -r /Source/ /Destination/
folderParam=$(basename !:3)
//commandResultvar : here I want to store status of rsync command
  • 1
    I rolled back your edit since that described a separate issue. Please accept one of the answers below since they told you how to do what you had originally asked for and ask a new question about the bad word specifier issue. Make sure you show us your script so we know what's on line 7 and please don't post images of text. Just copy the text directly from the terminal into your question and use the formatting tools to make it look like code. – terdon Oct 12 '16 at 9:14
  • @terdon Thanks but both answer do not provide the solution – HDev007 Oct 12 '16 at 9:27
  • They do. The way to get the exit status is to save it in a variable and then echo the variable. You are using it in a script which also does something else and that is where the issue is. – terdon Oct 12 '16 at 9:28
  • @terdon I already mentioned in my question I know $? gives status of previous command and I first ran the solution on command line and got the same error. – HDev007 Oct 12 '16 at 9:32
  • Let's discuss this in /dev/chat. – terdon Oct 12 '16 at 9:34
rsync -avh -r /Source/ /Destination/
folderParam=$(basename !:3)
# use ${rsync_status} here...
  • I tried your solution but folderParam is not providing the expected result ..it give errors :3 bad word specifier – HDev007 Oct 12 '16 at 9:41

It is not possible to get an exit code from particular command. You can get it only from the previous one. But there is nothing preventing you from storing this value into different variable and use it later:

rsync -avh -r /Source/ /Destination/
folderParam=$(basename !:3)
# $EXIT_CODE contains the exit code of the rsync command
  • Similar solution to Kamaraj and same issue. – HDev007 Oct 12 '16 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.