Determining the exit status of the “return” built-in
up vote
3
down vote
favorite
I was reading about traps, but only return works for me in my shell script, so was wondering what status or code it returns, so what I tried is,
#!/bin/bash
seeOutput=`return`
echo $seeOutput
It's just returning a new line and when done on terminal, it says,
-bash: return: can only `return' from a function or sourced script
which I already know :p I just need to know "return"'s exit status.
bash shell exit
|
show 1 more comment
up vote
3
down vote
favorite
I was reading about traps, but only return works for me in my shell script, so was wondering what status or code it returns, so what I tried is,
#!/bin/bash
seeOutput=`return`
echo $seeOutput
It's just returning a new line and when done on terminal, it says,
-bash: return: can only `return' from a function or sourced script
which I already know :p I just need to know "return"'s exit status.
bash shell exit
return
defaults to true. bash 4.3 has includedreturn -1
which means error. Thusreturn
accept negative values as return value (e.g. return -1 will show as (8 bit) 255 in the caller). See wiki.bash-hackers.org/scripting/bashchanges
– Valentin Bajrami
Aug 6 '14 at 12:35
There's a good answer on StackOverflow for this question.
– garethTheRed
Aug 6 '14 at 12:39
1
@val0x00ff, no,return
defaults toreturn "$?"
(that is, it returns with the exit status of the last run command).
– Stéphane Chazelas
Aug 6 '14 at 12:42
@StéphaneChazelas What I was meaning to say. Usuallyreturn
on its own indicates true as you'd do in creturn 0
to indicate success. Anything else will be false and ofcourse based on the status of$?
returned from the last command. e.gf(){ args=2; [[ $# = $args ]] && return || return ; }; f 1
Will hit the secondreturn
which will be false. Howeverf(){ args=2; [[ $# = $args ]] && return || return ; }; f 1 2
will hit the firstreturn
which will be true.
– Valentin Bajrami
Aug 6 '14 at 13:02
Since you know that you can only callreturn
from a function or a sourced script, why are you calling it in another context? What do you expect to happen? And what do you mean by “ "return"'s exit status”? Sincereturn
causes its context to exit, there's no way to observe any exit status of thereturn
instruction itself, what you observe is the exit status of the containing function or sourced script.
– Gilles
Aug 6 '14 at 23:01
|
show 1 more comment
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I was reading about traps, but only return works for me in my shell script, so was wondering what status or code it returns, so what I tried is,
#!/bin/bash
seeOutput=`return`
echo $seeOutput
It's just returning a new line and when done on terminal, it says,
-bash: return: can only `return' from a function or sourced script
which I already know :p I just need to know "return"'s exit status.
bash shell exit
I was reading about traps, but only return works for me in my shell script, so was wondering what status or code it returns, so what I tried is,
#!/bin/bash
seeOutput=`return`
echo $seeOutput
It's just returning a new line and when done on terminal, it says,
-bash: return: can only `return' from a function or sourced script
which I already know :p I just need to know "return"'s exit status.
bash shell exit
bash shell exit
edited 2 days ago
Rui F Ribeiro
38.2k1475125
38.2k1475125
asked Aug 6 '14 at 12:27
Keyshov Borate
67911124
67911124
return
defaults to true. bash 4.3 has includedreturn -1
which means error. Thusreturn
accept negative values as return value (e.g. return -1 will show as (8 bit) 255 in the caller). See wiki.bash-hackers.org/scripting/bashchanges
– Valentin Bajrami
Aug 6 '14 at 12:35
There's a good answer on StackOverflow for this question.
– garethTheRed
Aug 6 '14 at 12:39
1
@val0x00ff, no,return
defaults toreturn "$?"
(that is, it returns with the exit status of the last run command).
– Stéphane Chazelas
Aug 6 '14 at 12:42
@StéphaneChazelas What I was meaning to say. Usuallyreturn
on its own indicates true as you'd do in creturn 0
to indicate success. Anything else will be false and ofcourse based on the status of$?
returned from the last command. e.gf(){ args=2; [[ $# = $args ]] && return || return ; }; f 1
Will hit the secondreturn
which will be false. Howeverf(){ args=2; [[ $# = $args ]] && return || return ; }; f 1 2
will hit the firstreturn
which will be true.
– Valentin Bajrami
Aug 6 '14 at 13:02
Since you know that you can only callreturn
from a function or a sourced script, why are you calling it in another context? What do you expect to happen? And what do you mean by “ "return"'s exit status”? Sincereturn
causes its context to exit, there's no way to observe any exit status of thereturn
instruction itself, what you observe is the exit status of the containing function or sourced script.
– Gilles
Aug 6 '14 at 23:01
|
show 1 more comment
return
defaults to true. bash 4.3 has includedreturn -1
which means error. Thusreturn
accept negative values as return value (e.g. return -1 will show as (8 bit) 255 in the caller). See wiki.bash-hackers.org/scripting/bashchanges
– Valentin Bajrami
Aug 6 '14 at 12:35
There's a good answer on StackOverflow for this question.
– garethTheRed
Aug 6 '14 at 12:39
1
@val0x00ff, no,return
defaults toreturn "$?"
(that is, it returns with the exit status of the last run command).
– Stéphane Chazelas
Aug 6 '14 at 12:42
@StéphaneChazelas What I was meaning to say. Usuallyreturn
on its own indicates true as you'd do in creturn 0
to indicate success. Anything else will be false and ofcourse based on the status of$?
returned from the last command. e.gf(){ args=2; [[ $# = $args ]] && return || return ; }; f 1
Will hit the secondreturn
which will be false. Howeverf(){ args=2; [[ $# = $args ]] && return || return ; }; f 1 2
will hit the firstreturn
which will be true.
– Valentin Bajrami
Aug 6 '14 at 13:02
Since you know that you can only callreturn
from a function or a sourced script, why are you calling it in another context? What do you expect to happen? And what do you mean by “ "return"'s exit status”? Sincereturn
causes its context to exit, there's no way to observe any exit status of thereturn
instruction itself, what you observe is the exit status of the containing function or sourced script.
– Gilles
Aug 6 '14 at 23:01
return
defaults to true. bash 4.3 has included return -1
which means error. Thus return
accept negative values as return value (e.g. return -1 will show as (8 bit) 255 in the caller). See wiki.bash-hackers.org/scripting/bashchanges– Valentin Bajrami
Aug 6 '14 at 12:35
return
defaults to true. bash 4.3 has included return -1
which means error. Thus return
accept negative values as return value (e.g. return -1 will show as (8 bit) 255 in the caller). See wiki.bash-hackers.org/scripting/bashchanges– Valentin Bajrami
Aug 6 '14 at 12:35
There's a good answer on StackOverflow for this question.
– garethTheRed
Aug 6 '14 at 12:39
There's a good answer on StackOverflow for this question.
– garethTheRed
Aug 6 '14 at 12:39
1
1
@val0x00ff, no,
return
defaults to return "$?"
(that is, it returns with the exit status of the last run command).– Stéphane Chazelas
Aug 6 '14 at 12:42
@val0x00ff, no,
return
defaults to return "$?"
(that is, it returns with the exit status of the last run command).– Stéphane Chazelas
Aug 6 '14 at 12:42
@StéphaneChazelas What I was meaning to say. Usually
return
on its own indicates true as you'd do in c return 0
to indicate success. Anything else will be false and ofcourse based on the status of $?
returned from the last command. e.g f(){ args=2; [[ $# = $args ]] && return || return ; }; f 1
Will hit the second return
which will be false. However f(){ args=2; [[ $# = $args ]] && return || return ; }; f 1 2
will hit the first return
which will be true.– Valentin Bajrami
Aug 6 '14 at 13:02
@StéphaneChazelas What I was meaning to say. Usually
return
on its own indicates true as you'd do in c return 0
to indicate success. Anything else will be false and ofcourse based on the status of $?
returned from the last command. e.g f(){ args=2; [[ $# = $args ]] && return || return ; }; f 1
Will hit the second return
which will be false. However f(){ args=2; [[ $# = $args ]] && return || return ; }; f 1 2
will hit the first return
which will be true.– Valentin Bajrami
Aug 6 '14 at 13:02
Since you know that you can only call
return
from a function or a sourced script, why are you calling it in another context? What do you expect to happen? And what do you mean by “ "return"'s exit status”? Since return
causes its context to exit, there's no way to observe any exit status of the return
instruction itself, what you observe is the exit status of the containing function or sourced script.– Gilles
Aug 6 '14 at 23:01
Since you know that you can only call
return
from a function or a sourced script, why are you calling it in another context? What do you expect to happen? And what do you mean by “ "return"'s exit status”? Since return
causes its context to exit, there's no way to observe any exit status of the return
instruction itself, what you observe is the exit status of the containing function or sourced script.– Gilles
Aug 6 '14 at 23:01
|
show 1 more comment
3 Answers
3
active
oldest
votes
up vote
3
down vote
accepted
return code is stored in $? variable.
false ; echo $?
true ; echo $?
would return
1
0
unix convention is that 0 means OK.
in your exemple, seeOuput hold whatever output from the back quoted command.
Do not mistake output and return code.
add a comment |
up vote
4
down vote
What you're doing is calling a shell command return
which doesn't make sense.
In general, return
with no value followed returns the exit status of the last command executed.
From man
: Causes a function to exit with the return value specified by n. If n is omitted, the return status is that of the last command executed in the function body.
But actually I was using return to just to get out of my script(If one of the command-line argument is missing) Thanks for adding this too to my knowledge! :)
– Keyshov Borate
Aug 6 '14 at 12:48
add a comment |
up vote
-1
down vote
This is how I used it:
f()
{
ls $AAA
return $?
}
g()
{
f
return $?
}
d()
{
g
echo $?
}
AAA=
d
_
<contents of dir>
0
_
AAA=sdsasdasd
d
_
ls: sdsasdasd: No such file or directory
2
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
return code is stored in $? variable.
false ; echo $?
true ; echo $?
would return
1
0
unix convention is that 0 means OK.
in your exemple, seeOuput hold whatever output from the back quoted command.
Do not mistake output and return code.
add a comment |
up vote
3
down vote
accepted
return code is stored in $? variable.
false ; echo $?
true ; echo $?
would return
1
0
unix convention is that 0 means OK.
in your exemple, seeOuput hold whatever output from the back quoted command.
Do not mistake output and return code.
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
return code is stored in $? variable.
false ; echo $?
true ; echo $?
would return
1
0
unix convention is that 0 means OK.
in your exemple, seeOuput hold whatever output from the back quoted command.
Do not mistake output and return code.
return code is stored in $? variable.
false ; echo $?
true ; echo $?
would return
1
0
unix convention is that 0 means OK.
in your exemple, seeOuput hold whatever output from the back quoted command.
Do not mistake output and return code.
answered Aug 6 '14 at 12:36
Archemar
19.4k93468
19.4k93468
add a comment |
add a comment |
up vote
4
down vote
What you're doing is calling a shell command return
which doesn't make sense.
In general, return
with no value followed returns the exit status of the last command executed.
From man
: Causes a function to exit with the return value specified by n. If n is omitted, the return status is that of the last command executed in the function body.
But actually I was using return to just to get out of my script(If one of the command-line argument is missing) Thanks for adding this too to my knowledge! :)
– Keyshov Borate
Aug 6 '14 at 12:48
add a comment |
up vote
4
down vote
What you're doing is calling a shell command return
which doesn't make sense.
In general, return
with no value followed returns the exit status of the last command executed.
From man
: Causes a function to exit with the return value specified by n. If n is omitted, the return status is that of the last command executed in the function body.
But actually I was using return to just to get out of my script(If one of the command-line argument is missing) Thanks for adding this too to my knowledge! :)
– Keyshov Borate
Aug 6 '14 at 12:48
add a comment |
up vote
4
down vote
up vote
4
down vote
What you're doing is calling a shell command return
which doesn't make sense.
In general, return
with no value followed returns the exit status of the last command executed.
From man
: Causes a function to exit with the return value specified by n. If n is omitted, the return status is that of the last command executed in the function body.
What you're doing is calling a shell command return
which doesn't make sense.
In general, return
with no value followed returns the exit status of the last command executed.
From man
: Causes a function to exit with the return value specified by n. If n is omitted, the return status is that of the last command executed in the function body.
answered Aug 6 '14 at 12:38
csny
6632721
6632721
But actually I was using return to just to get out of my script(If one of the command-line argument is missing) Thanks for adding this too to my knowledge! :)
– Keyshov Borate
Aug 6 '14 at 12:48
add a comment |
But actually I was using return to just to get out of my script(If one of the command-line argument is missing) Thanks for adding this too to my knowledge! :)
– Keyshov Borate
Aug 6 '14 at 12:48
But actually I was using return to just to get out of my script(If one of the command-line argument is missing) Thanks for adding this too to my knowledge! :)
– Keyshov Borate
Aug 6 '14 at 12:48
But actually I was using return to just to get out of my script(If one of the command-line argument is missing) Thanks for adding this too to my knowledge! :)
– Keyshov Borate
Aug 6 '14 at 12:48
add a comment |
up vote
-1
down vote
This is how I used it:
f()
{
ls $AAA
return $?
}
g()
{
f
return $?
}
d()
{
g
echo $?
}
AAA=
d
_
<contents of dir>
0
_
AAA=sdsasdasd
d
_
ls: sdsasdasd: No such file or directory
2
add a comment |
up vote
-1
down vote
This is how I used it:
f()
{
ls $AAA
return $?
}
g()
{
f
return $?
}
d()
{
g
echo $?
}
AAA=
d
_
<contents of dir>
0
_
AAA=sdsasdasd
d
_
ls: sdsasdasd: No such file or directory
2
add a comment |
up vote
-1
down vote
up vote
-1
down vote
This is how I used it:
f()
{
ls $AAA
return $?
}
g()
{
f
return $?
}
d()
{
g
echo $?
}
AAA=
d
_
<contents of dir>
0
_
AAA=sdsasdasd
d
_
ls: sdsasdasd: No such file or directory
2
This is how I used it:
f()
{
ls $AAA
return $?
}
g()
{
f
return $?
}
d()
{
g
echo $?
}
AAA=
d
_
<contents of dir>
0
_
AAA=sdsasdasd
d
_
ls: sdsasdasd: No such file or directory
2
answered Feb 26 '15 at 21:52
Bohdan
992
992
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f148723%2fdetermining-the-exit-status-of-the-return-built-in%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
return
defaults to true. bash 4.3 has includedreturn -1
which means error. Thusreturn
accept negative values as return value (e.g. return -1 will show as (8 bit) 255 in the caller). See wiki.bash-hackers.org/scripting/bashchanges– Valentin Bajrami
Aug 6 '14 at 12:35
There's a good answer on StackOverflow for this question.
– garethTheRed
Aug 6 '14 at 12:39
1
@val0x00ff, no,
return
defaults toreturn "$?"
(that is, it returns with the exit status of the last run command).– Stéphane Chazelas
Aug 6 '14 at 12:42
@StéphaneChazelas What I was meaning to say. Usually
return
on its own indicates true as you'd do in creturn 0
to indicate success. Anything else will be false and ofcourse based on the status of$?
returned from the last command. e.gf(){ args=2; [[ $# = $args ]] && return || return ; }; f 1
Will hit the secondreturn
which will be false. Howeverf(){ args=2; [[ $# = $args ]] && return || return ; }; f 1 2
will hit the firstreturn
which will be true.– Valentin Bajrami
Aug 6 '14 at 13:02
Since you know that you can only call
return
from a function or a sourced script, why are you calling it in another context? What do you expect to happen? And what do you mean by “ "return"'s exit status”? Sincereturn
causes its context to exit, there's no way to observe any exit status of thereturn
instruction itself, what you observe is the exit status of the containing function or sourced script.– Gilles
Aug 6 '14 at 23:01