3

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
5
  • 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, 2016 at 9:14
  • @terdon Thanks but both answer do not provide the solution
    – HDev007
    Oct 12, 2016 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, 2016 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, 2016 at 9:32
  • Let's discuss this in /dev/chat.
    – terdon
    Oct 12, 2016 at 9:34

2 Answers 2

2
rsync -avh -r /Source/ /Destination/
rsync_status=$?
folderParam=$(basename !:3)
# use ${rsync_status} here...
1
  • I tried your solution but folderParam is not providing the expected result ..it give errors :3 bad word specifier
    – HDev007
    Oct 12, 2016 at 9:41
0

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/
EXIT_CODE=$?
folderParam=$(basename !:3)
# $EXIT_CODE contains the exit code of the rsync command
1
  • Similar solution to Kamaraj and same issue.
    – HDev007
    Oct 12, 2016 at 9:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .