Capture exit code and output of a command
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
I'd like to do:
1.sh
:
#!/usr/bin/env bash
set -eu
r=0
a=$(./2.sh || r=$?)
echo "$a"
echo "$r"
2.sh
:
#!/usr/bin/env bash
echo output
exit 2
But it outputs:
$ ./1.sh
output
0 # I'd like to have `2` here
Since $(...)
runs a separate shell. So, how do I capture both, exit code and output?
shell command-substitution return-status
add a comment |Â
up vote
3
down vote
favorite
I'd like to do:
1.sh
:
#!/usr/bin/env bash
set -eu
r=0
a=$(./2.sh || r=$?)
echo "$a"
echo "$r"
2.sh
:
#!/usr/bin/env bash
echo output
exit 2
But it outputs:
$ ./1.sh
output
0 # I'd like to have `2` here
Since $(...)
runs a separate shell. So, how do I capture both, exit code and output?
shell command-substitution return-status
a=$(./2.sh); r=$?; ## doesn't work?
â Jeff Schaller
Mar 4 '16 at 11:34
The reason1.sh
has an exit code of 0 not 2 is becauseecho "$r"
is a command that exits 0.
â Mikkel
May 29 at 14:20
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I'd like to do:
1.sh
:
#!/usr/bin/env bash
set -eu
r=0
a=$(./2.sh || r=$?)
echo "$a"
echo "$r"
2.sh
:
#!/usr/bin/env bash
echo output
exit 2
But it outputs:
$ ./1.sh
output
0 # I'd like to have `2` here
Since $(...)
runs a separate shell. So, how do I capture both, exit code and output?
shell command-substitution return-status
I'd like to do:
1.sh
:
#!/usr/bin/env bash
set -eu
r=0
a=$(./2.sh || r=$?)
echo "$a"
echo "$r"
2.sh
:
#!/usr/bin/env bash
echo output
exit 2
But it outputs:
$ ./1.sh
output
0 # I'd like to have `2` here
Since $(...)
runs a separate shell. So, how do I capture both, exit code and output?
shell command-substitution return-status
shell command-substitution return-status
edited Aug 26 at 14:24
Peter Mortensen
79748
79748
asked Mar 4 '16 at 11:28
x-yuri
1,11911641
1,11911641
a=$(./2.sh); r=$?; ## doesn't work?
â Jeff Schaller
Mar 4 '16 at 11:34
The reason1.sh
has an exit code of 0 not 2 is becauseecho "$r"
is a command that exits 0.
â Mikkel
May 29 at 14:20
add a comment |Â
a=$(./2.sh); r=$?; ## doesn't work?
â Jeff Schaller
Mar 4 '16 at 11:34
The reason1.sh
has an exit code of 0 not 2 is becauseecho "$r"
is a command that exits 0.
â Mikkel
May 29 at 14:20
a=$(./2.sh); r=$?; ## doesn't work?
â Jeff Schaller
Mar 4 '16 at 11:34
a=$(./2.sh); r=$?; ## doesn't work?
â Jeff Schaller
Mar 4 '16 at 11:34
The reason
1.sh
has an exit code of 0 not 2 is because echo "$r"
is a command that exits 0.â Mikkel
May 29 at 14:20
The reason
1.sh
has an exit code of 0 not 2 is because echo "$r"
is a command that exits 0.â Mikkel
May 29 at 14:20
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
8
down vote
accepted
The exit code of a process calling another process is the one of the called process.
$($($($($(exit 2)))))
echo $?
2
Here there are 5 levels of calling.
In your case:
r=0
a=$(./2.sh)
r=$?
echo "$a"
echo "$r"
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
accepted
The exit code of a process calling another process is the one of the called process.
$($($($($(exit 2)))))
echo $?
2
Here there are 5 levels of calling.
In your case:
r=0
a=$(./2.sh)
r=$?
echo "$a"
echo "$r"
add a comment |Â
up vote
8
down vote
accepted
The exit code of a process calling another process is the one of the called process.
$($($($($(exit 2)))))
echo $?
2
Here there are 5 levels of calling.
In your case:
r=0
a=$(./2.sh)
r=$?
echo "$a"
echo "$r"
add a comment |Â
up vote
8
down vote
accepted
up vote
8
down vote
accepted
The exit code of a process calling another process is the one of the called process.
$($($($($(exit 2)))))
echo $?
2
Here there are 5 levels of calling.
In your case:
r=0
a=$(./2.sh)
r=$?
echo "$a"
echo "$r"
The exit code of a process calling another process is the one of the called process.
$($($($($(exit 2)))))
echo $?
2
Here there are 5 levels of calling.
In your case:
r=0
a=$(./2.sh)
r=$?
echo "$a"
echo "$r"
edited Mar 4 '16 at 12:10
Jeff Schaller
32.7k849110
32.7k849110
answered Mar 4 '16 at 11:36
Emmanuel
2,92911020
2,92911020
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f267593%2fcapture-exit-code-and-output-of-a-command%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
a=$(./2.sh); r=$?; ## doesn't work?
â Jeff Schaller
Mar 4 '16 at 11:34
The reason
1.sh
has an exit code of 0 not 2 is becauseecho "$r"
is a command that exits 0.â Mikkel
May 29 at 14:20