Capturing return code with test []

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite
1












If I run this:



#!/usr/bin/env bash

simple_return_zero()
return 0;


simple_return_one()
return 1;


if [ simple_return_zero ]; then
echo "we have 0000";
fi

if [ ! simple_return_zero ]; then
echo "we have not 00000";
fi

if [ simple_return_one ]; then
echo "we have 11111";
fi

if [ ! simple_return_one ]; then
echo "we have not 11111";
fi


I get:



we have 0000
we have 11111


I know the above is the wrong code to use, I think this is the right way to do it:



if simple_return_zero; then
echo "we have 0000";
fi

if ! simple_return_zero; then
echo "we have not 00000";
fi

if simple_return_one; then
echo "we have 11111";
fi

if ! simple_return_one; then
echo "we have not 11111";
fi


and now we get something more expected:



we have 0000
we have not 11111


My question is - why doesn't the test command ( [ ] ) work in this case? Doesn't the test command check for exit codes / return codes???







share|improve this question



















  • also, if there is a better way to run/organize the code, pls lmk...it's a little awkward with the ! in front of the command without any [ ] braces.
    – Alexander Mills
    Jun 14 at 0:42














up vote
1
down vote

favorite
1












If I run this:



#!/usr/bin/env bash

simple_return_zero()
return 0;


simple_return_one()
return 1;


if [ simple_return_zero ]; then
echo "we have 0000";
fi

if [ ! simple_return_zero ]; then
echo "we have not 00000";
fi

if [ simple_return_one ]; then
echo "we have 11111";
fi

if [ ! simple_return_one ]; then
echo "we have not 11111";
fi


I get:



we have 0000
we have 11111


I know the above is the wrong code to use, I think this is the right way to do it:



if simple_return_zero; then
echo "we have 0000";
fi

if ! simple_return_zero; then
echo "we have not 00000";
fi

if simple_return_one; then
echo "we have 11111";
fi

if ! simple_return_one; then
echo "we have not 11111";
fi


and now we get something more expected:



we have 0000
we have not 11111


My question is - why doesn't the test command ( [ ] ) work in this case? Doesn't the test command check for exit codes / return codes???







share|improve this question



















  • also, if there is a better way to run/organize the code, pls lmk...it's a little awkward with the ! in front of the command without any [ ] braces.
    – Alexander Mills
    Jun 14 at 0:42












up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





If I run this:



#!/usr/bin/env bash

simple_return_zero()
return 0;


simple_return_one()
return 1;


if [ simple_return_zero ]; then
echo "we have 0000";
fi

if [ ! simple_return_zero ]; then
echo "we have not 00000";
fi

if [ simple_return_one ]; then
echo "we have 11111";
fi

if [ ! simple_return_one ]; then
echo "we have not 11111";
fi


I get:



we have 0000
we have 11111


I know the above is the wrong code to use, I think this is the right way to do it:



if simple_return_zero; then
echo "we have 0000";
fi

if ! simple_return_zero; then
echo "we have not 00000";
fi

if simple_return_one; then
echo "we have 11111";
fi

if ! simple_return_one; then
echo "we have not 11111";
fi


and now we get something more expected:



we have 0000
we have not 11111


My question is - why doesn't the test command ( [ ] ) work in this case? Doesn't the test command check for exit codes / return codes???







share|improve this question











If I run this:



#!/usr/bin/env bash

simple_return_zero()
return 0;


simple_return_one()
return 1;


if [ simple_return_zero ]; then
echo "we have 0000";
fi

if [ ! simple_return_zero ]; then
echo "we have not 00000";
fi

if [ simple_return_one ]; then
echo "we have 11111";
fi

if [ ! simple_return_one ]; then
echo "we have not 11111";
fi


I get:



we have 0000
we have 11111


I know the above is the wrong code to use, I think this is the right way to do it:



if simple_return_zero; then
echo "we have 0000";
fi

if ! simple_return_zero; then
echo "we have not 00000";
fi

if simple_return_one; then
echo "we have 11111";
fi

if ! simple_return_one; then
echo "we have not 11111";
fi


and now we get something more expected:



we have 0000
we have not 11111


My question is - why doesn't the test command ( [ ] ) work in this case? Doesn't the test command check for exit codes / return codes???









share|improve this question










share|improve this question




share|improve this question









asked Jun 14 at 0:41









Alexander Mills

1,873929




1,873929











  • also, if there is a better way to run/organize the code, pls lmk...it's a little awkward with the ! in front of the command without any [ ] braces.
    – Alexander Mills
    Jun 14 at 0:42
















  • also, if there is a better way to run/organize the code, pls lmk...it's a little awkward with the ! in front of the command without any [ ] braces.
    – Alexander Mills
    Jun 14 at 0:42















also, if there is a better way to run/organize the code, pls lmk...it's a little awkward with the ! in front of the command without any [ ] braces.
– Alexander Mills
Jun 14 at 0:42




also, if there is a better way to run/organize the code, pls lmk...it's a little awkward with the ! in front of the command without any [ ] braces.
– Alexander Mills
Jun 14 at 0:42










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted











Doesn't the test command check for exit codes / return codes???




Absolutely not. It performs the test as defined by the text within the brackets, whose syntax can be viewed via help test.



if on its own checks the return code of the command executed.






share|improve this answer





















  • humma but what does test actually do if there is no comparison operator? for example if it's just a command in between the brackets?
    – Alexander Mills
    Jun 14 at 0:51










  • From help test: "STRING True if string is not empty."
    – Ignacio Vazquez-Abrams
    Jun 14 at 0:51










  • Ah yes that makes sense, fml
    – Alexander Mills
    Jun 14 at 0:52










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f449696%2fcapturing-return-code-with-test%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted











Doesn't the test command check for exit codes / return codes???




Absolutely not. It performs the test as defined by the text within the brackets, whose syntax can be viewed via help test.



if on its own checks the return code of the command executed.






share|improve this answer





















  • humma but what does test actually do if there is no comparison operator? for example if it's just a command in between the brackets?
    – Alexander Mills
    Jun 14 at 0:51










  • From help test: "STRING True if string is not empty."
    – Ignacio Vazquez-Abrams
    Jun 14 at 0:51










  • Ah yes that makes sense, fml
    – Alexander Mills
    Jun 14 at 0:52














up vote
2
down vote



accepted











Doesn't the test command check for exit codes / return codes???




Absolutely not. It performs the test as defined by the text within the brackets, whose syntax can be viewed via help test.



if on its own checks the return code of the command executed.






share|improve this answer





















  • humma but what does test actually do if there is no comparison operator? for example if it's just a command in between the brackets?
    – Alexander Mills
    Jun 14 at 0:51










  • From help test: "STRING True if string is not empty."
    – Ignacio Vazquez-Abrams
    Jun 14 at 0:51










  • Ah yes that makes sense, fml
    – Alexander Mills
    Jun 14 at 0:52












up vote
2
down vote



accepted







up vote
2
down vote



accepted







Doesn't the test command check for exit codes / return codes???




Absolutely not. It performs the test as defined by the text within the brackets, whose syntax can be viewed via help test.



if on its own checks the return code of the command executed.






share|improve this answer














Doesn't the test command check for exit codes / return codes???




Absolutely not. It performs the test as defined by the text within the brackets, whose syntax can be viewed via help test.



if on its own checks the return code of the command executed.







share|improve this answer













share|improve this answer



share|improve this answer











answered Jun 14 at 0:46









Ignacio Vazquez-Abrams

31.9k66780




31.9k66780











  • humma but what does test actually do if there is no comparison operator? for example if it's just a command in between the brackets?
    – Alexander Mills
    Jun 14 at 0:51










  • From help test: "STRING True if string is not empty."
    – Ignacio Vazquez-Abrams
    Jun 14 at 0:51










  • Ah yes that makes sense, fml
    – Alexander Mills
    Jun 14 at 0:52
















  • humma but what does test actually do if there is no comparison operator? for example if it's just a command in between the brackets?
    – Alexander Mills
    Jun 14 at 0:51










  • From help test: "STRING True if string is not empty."
    – Ignacio Vazquez-Abrams
    Jun 14 at 0:51










  • Ah yes that makes sense, fml
    – Alexander Mills
    Jun 14 at 0:52















humma but what does test actually do if there is no comparison operator? for example if it's just a command in between the brackets?
– Alexander Mills
Jun 14 at 0:51




humma but what does test actually do if there is no comparison operator? for example if it's just a command in between the brackets?
– Alexander Mills
Jun 14 at 0:51












From help test: "STRING True if string is not empty."
– Ignacio Vazquez-Abrams
Jun 14 at 0:51




From help test: "STRING True if string is not empty."
– Ignacio Vazquez-Abrams
Jun 14 at 0:51












Ah yes that makes sense, fml
– Alexander Mills
Jun 14 at 0:52




Ah yes that makes sense, fml
– Alexander Mills
Jun 14 at 0:52












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f449696%2fcapturing-return-code-with-test%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay