# Read exit value from Shell script

**URL:** <https://community.unix.com/t/read-exit-value-from-shell-script/367444>\
**Category:** Shell Programming and Scripting\
**Created:** [July 14, 2017, 11:48pm UTC](https://community.unix.com/t/read-exit-value-from-shell-script/367444 "2017-07-14T23:48:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![baluchen](https://community.unix.com/letter_avatar/baluchen/32/6_449c533f586939b6e5f246632a46421f.png) [@baluchen](https://community.unix.com/u/baluchen)\
**Post date:** [July 14, 2017, 11:48pm UTC](https://community.unix.com/t/read-exit-value-from-shell-script/367444/1 "2017-07-14T23:48:50Z")

</div>

Hi all,

I have a situation to read exit value of a command (not exit code) and process further.

```nohighlight
bash-4.2$ returnvalue=`ssh DOMAIN\\\\user1@10.7.7.68 'cmd /c "del C:\Users\user1\db_test.bak"'`
Could Not Find C:\Users\user1\db_test.bak
bash-4.2$ echo $?
0
bash-4.2$ echo $returnvalue
bash-4.2$

```

I intentionally made "db\_test.bak" not available because i want the return value to be captured.

The expectation is the return value " `Could Not Find C:\Users\user1\db_test.bak` "to be assigned in a variable.

Appreciate your help

Thanks  
Bala

---

<div class="post-metadata">

**Author:** ![RavinderSingh13](https://community.unix.com/letter_avatar/ravindersingh13/32/6_449c533f586939b6e5f246632a46421f.png) [@RavinderSingh13](https://community.unix.com/u/RavinderSingh13)\
**Post date:** [July 15, 2017, 1:34am UTC](https://community.unix.com/t/read-exit-value-from-shell-script/367444/2 "2017-07-15T01:34:56Z")

</div>

Hello baluchen,

Could you please try following and let me know if this helps you.

```nohighlight
ssh user_name@host "commands" 2>error_output

```

Now above will capture the errors if any will come from commands, if you want to see them then you could run following command.

```nohighlight
cat error_output >&2

```

Let me know how it goes it then.

Thanks,  
R. Singh

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [July 15, 2017, 3:43am UTC](https://community.unix.com/t/read-exit-value-from-shell-script/367444/3 "2017-07-15T03:43:04Z")

</div>

Try

```nohighlight
returnvalue=`ssh DOMAIN\\\\user1@10.7.7.68 'cmd /c "del C:\Users\user1\db_test.bak"' 2>&1`

```

although I'm not too sure how MS handles error msgs to stderr nor how it assigns exit codes.

---

<div class="post-metadata">

**Author:** ![baluchen](https://community.unix.com/letter_avatar/baluchen/32/6_449c533f586939b6e5f246632a46421f.png) [@baluchen](https://community.unix.com/u/baluchen)\
**Post date:** [July 15, 2017, 9:16am UTC](https://community.unix.com/t/read-exit-value-from-shell-script/367444/4 "2017-07-15T09:16:58Z")

</div>

> [@ravindersingh13](#):
>
> Hello baluchen,
> 
> Could you please try following and let me know if this helps you.
> 
> ```plaintext
> ssh user_name@host "commands" 2>error_output
> 
> ```
> 
> Now above will capture the errors if any will come from commands, if you want to see them then you could run following command.
> 
> ```plaintext
> cat error_output >&2
> 
> ```
> 
> Let me know how it goes it then.
> 
> Thanks,  
> R. Singh

Thank you Singh. It worked as expected.

Thank you  
Bala
