Bash assign variable result of bool function, then check

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











up vote
4
down vote

favorite












I am trying to create a function which returns 0 or 1 (i.e. true or false) and takes an argument, then create a variable in another which stores the results of that function. Finally check if that variable is 0 or 1 (true or false)



Here is a sample of what I am attempting



#!/bin/bash

_has_string()
if [ $1 == "string" ];
return 0
else
return 1
fi


_my_func()
var=$(_has_string "string")
if [ $var == "0" ]; then
echo "var contains string"
else
echo "var does not contain string"
fi


_my_func


I have tried a few variations of this and can not seem to find a way to get it to work. All of my variations basically just return the $var as nothing. Not a 0. Not null. Literally it is just blank.







share|improve this question















  • 1




    Note: In Bash if comparing strings, you must use double brackets like [[ $1 == "string" ]];
    – Vlastimil
    May 28 at 7:02














up vote
4
down vote

favorite












I am trying to create a function which returns 0 or 1 (i.e. true or false) and takes an argument, then create a variable in another which stores the results of that function. Finally check if that variable is 0 or 1 (true or false)



Here is a sample of what I am attempting



#!/bin/bash

_has_string()
if [ $1 == "string" ];
return 0
else
return 1
fi


_my_func()
var=$(_has_string "string")
if [ $var == "0" ]; then
echo "var contains string"
else
echo "var does not contain string"
fi


_my_func


I have tried a few variations of this and can not seem to find a way to get it to work. All of my variations basically just return the $var as nothing. Not a 0. Not null. Literally it is just blank.







share|improve this question















  • 1




    Note: In Bash if comparing strings, you must use double brackets like [[ $1 == "string" ]];
    – Vlastimil
    May 28 at 7:02












up vote
4
down vote

favorite









up vote
4
down vote

favorite











I am trying to create a function which returns 0 or 1 (i.e. true or false) and takes an argument, then create a variable in another which stores the results of that function. Finally check if that variable is 0 or 1 (true or false)



Here is a sample of what I am attempting



#!/bin/bash

_has_string()
if [ $1 == "string" ];
return 0
else
return 1
fi


_my_func()
var=$(_has_string "string")
if [ $var == "0" ]; then
echo "var contains string"
else
echo "var does not contain string"
fi


_my_func


I have tried a few variations of this and can not seem to find a way to get it to work. All of my variations basically just return the $var as nothing. Not a 0. Not null. Literally it is just blank.







share|improve this question











I am trying to create a function which returns 0 or 1 (i.e. true or false) and takes an argument, then create a variable in another which stores the results of that function. Finally check if that variable is 0 or 1 (true or false)



Here is a sample of what I am attempting



#!/bin/bash

_has_string()
if [ $1 == "string" ];
return 0
else
return 1
fi


_my_func()
var=$(_has_string "string")
if [ $var == "0" ]; then
echo "var contains string"
else
echo "var does not contain string"
fi


_my_func


I have tried a few variations of this and can not seem to find a way to get it to work. All of my variations basically just return the $var as nothing. Not a 0. Not null. Literally it is just blank.









share|improve this question










share|improve this question




share|improve this question









asked May 27 at 17:22









Byron Mansfield

628




628







  • 1




    Note: In Bash if comparing strings, you must use double brackets like [[ $1 == "string" ]];
    – Vlastimil
    May 28 at 7:02












  • 1




    Note: In Bash if comparing strings, you must use double brackets like [[ $1 == "string" ]];
    – Vlastimil
    May 28 at 7:02







1




1




Note: In Bash if comparing strings, you must use double brackets like [[ $1 == "string" ]];
– Vlastimil
May 28 at 7:02




Note: In Bash if comparing strings, you must use double brackets like [[ $1 == "string" ]];
– Vlastimil
May 28 at 7:02










3 Answers
3






active

oldest

votes

















up vote
13
down vote



accepted










You confuse output with the exit code.



_my_func() {
if _has_string 'string'; then


You should also quote your variables; and _has_string can be simplified:



_has_string() 
[ "$1" = 'string' ]






share|improve this answer























  • You are correct, I was confusing output with exit codes. Your solution works. As I had mentioned in original post I tried many variations, though I think the missing one that I kept getting wrong was putting square brackets around the if _has_string 'string' conditional. Removing the brackets made it worked. This is why I was attempting to store it in a variable. Your approach removes the need for creating an unneeded variable. Thank you.
    – Byron Mansfield
    May 29 at 20:21

















up vote
5
down vote













$(...) returns the output of the command: the things sent to stdout. You want the exit code. After a process (or, in your case, function) exits in a bash script, the special variable $? is set to the exit code.



So rather than



var=$(_has_string 'string')
...


consider



_has_string 'string'
var=$?
...





share|improve this answer

















  • 2




    Or even if _has_string 'string'; then ...
    – roaima
    May 27 at 20:42

















up vote
0
down vote













As others already pointed out, what you need to merely use the function within the if statement. Here's something you might not have known:



A function can be treated as a form of compound command. And according to bash manual [[ is one of the operators that can be used in construction of a compound command. Thus, your function _has_string() can be implemented as so:



$ _has_string()[[ "$1" = "string" ]]
$ if _has_string "string"; then echo "YES"; fi
YES


Of course, this is in no way portable, because of the [[ operator. But we can make use of ( ) or . Here's for example how this works in /bin/dash:



$ _has_string()( [ "$1" = "string" ]; )
$ if _has_string "string"; then echo "YES"; fi
YES
$ if _has_string "nope" ; then echo "YES"; fi
$





share|improve this answer

















  • 1




    I wouldn't use the parenthesis in that function definition, they run a subshell which is in no way needed here.
    – ilkkachu
    May 29 at 11:28










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%2f446339%2fbash-assign-variable-result-of-bool-function-then-check%23new-answer', 'question_page');

);

Post as a guest






























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
13
down vote



accepted










You confuse output with the exit code.



_my_func() {
if _has_string 'string'; then


You should also quote your variables; and _has_string can be simplified:



_has_string() 
[ "$1" = 'string' ]






share|improve this answer























  • You are correct, I was confusing output with exit codes. Your solution works. As I had mentioned in original post I tried many variations, though I think the missing one that I kept getting wrong was putting square brackets around the if _has_string 'string' conditional. Removing the brackets made it worked. This is why I was attempting to store it in a variable. Your approach removes the need for creating an unneeded variable. Thank you.
    – Byron Mansfield
    May 29 at 20:21














up vote
13
down vote



accepted










You confuse output with the exit code.



_my_func() {
if _has_string 'string'; then


You should also quote your variables; and _has_string can be simplified:



_has_string() 
[ "$1" = 'string' ]






share|improve this answer























  • You are correct, I was confusing output with exit codes. Your solution works. As I had mentioned in original post I tried many variations, though I think the missing one that I kept getting wrong was putting square brackets around the if _has_string 'string' conditional. Removing the brackets made it worked. This is why I was attempting to store it in a variable. Your approach removes the need for creating an unneeded variable. Thank you.
    – Byron Mansfield
    May 29 at 20:21












up vote
13
down vote



accepted







up vote
13
down vote



accepted






You confuse output with the exit code.



_my_func() {
if _has_string 'string'; then


You should also quote your variables; and _has_string can be simplified:



_has_string() 
[ "$1" = 'string' ]






share|improve this answer















You confuse output with the exit code.



_my_func() {
if _has_string 'string'; then


You should also quote your variables; and _has_string can be simplified:



_has_string() 
[ "$1" = 'string' ]







share|improve this answer















share|improve this answer



share|improve this answer








edited May 29 at 11:15









Jeff Schaller

31k846105




31k846105











answered May 27 at 17:31









Hauke Laging

53.1k1282130




53.1k1282130











  • You are correct, I was confusing output with exit codes. Your solution works. As I had mentioned in original post I tried many variations, though I think the missing one that I kept getting wrong was putting square brackets around the if _has_string 'string' conditional. Removing the brackets made it worked. This is why I was attempting to store it in a variable. Your approach removes the need for creating an unneeded variable. Thank you.
    – Byron Mansfield
    May 29 at 20:21
















  • You are correct, I was confusing output with exit codes. Your solution works. As I had mentioned in original post I tried many variations, though I think the missing one that I kept getting wrong was putting square brackets around the if _has_string 'string' conditional. Removing the brackets made it worked. This is why I was attempting to store it in a variable. Your approach removes the need for creating an unneeded variable. Thank you.
    – Byron Mansfield
    May 29 at 20:21















You are correct, I was confusing output with exit codes. Your solution works. As I had mentioned in original post I tried many variations, though I think the missing one that I kept getting wrong was putting square brackets around the if _has_string 'string' conditional. Removing the brackets made it worked. This is why I was attempting to store it in a variable. Your approach removes the need for creating an unneeded variable. Thank you.
– Byron Mansfield
May 29 at 20:21




You are correct, I was confusing output with exit codes. Your solution works. As I had mentioned in original post I tried many variations, though I think the missing one that I kept getting wrong was putting square brackets around the if _has_string 'string' conditional. Removing the brackets made it worked. This is why I was attempting to store it in a variable. Your approach removes the need for creating an unneeded variable. Thank you.
– Byron Mansfield
May 29 at 20:21












up vote
5
down vote













$(...) returns the output of the command: the things sent to stdout. You want the exit code. After a process (or, in your case, function) exits in a bash script, the special variable $? is set to the exit code.



So rather than



var=$(_has_string 'string')
...


consider



_has_string 'string'
var=$?
...





share|improve this answer

















  • 2




    Or even if _has_string 'string'; then ...
    – roaima
    May 27 at 20:42














up vote
5
down vote













$(...) returns the output of the command: the things sent to stdout. You want the exit code. After a process (or, in your case, function) exits in a bash script, the special variable $? is set to the exit code.



So rather than



var=$(_has_string 'string')
...


consider



_has_string 'string'
var=$?
...





share|improve this answer

















  • 2




    Or even if _has_string 'string'; then ...
    – roaima
    May 27 at 20:42












up vote
5
down vote










up vote
5
down vote









$(...) returns the output of the command: the things sent to stdout. You want the exit code. After a process (or, in your case, function) exits in a bash script, the special variable $? is set to the exit code.



So rather than



var=$(_has_string 'string')
...


consider



_has_string 'string'
var=$?
...





share|improve this answer













$(...) returns the output of the command: the things sent to stdout. You want the exit code. After a process (or, in your case, function) exits in a bash script, the special variable $? is set to the exit code.



So rather than



var=$(_has_string 'string')
...


consider



_has_string 'string'
var=$?
...






share|improve this answer













share|improve this answer



share|improve this answer











answered May 27 at 19:50









Silvio Mayolo

1513




1513







  • 2




    Or even if _has_string 'string'; then ...
    – roaima
    May 27 at 20:42












  • 2




    Or even if _has_string 'string'; then ...
    – roaima
    May 27 at 20:42







2




2




Or even if _has_string 'string'; then ...
– roaima
May 27 at 20:42




Or even if _has_string 'string'; then ...
– roaima
May 27 at 20:42










up vote
0
down vote













As others already pointed out, what you need to merely use the function within the if statement. Here's something you might not have known:



A function can be treated as a form of compound command. And according to bash manual [[ is one of the operators that can be used in construction of a compound command. Thus, your function _has_string() can be implemented as so:



$ _has_string()[[ "$1" = "string" ]]
$ if _has_string "string"; then echo "YES"; fi
YES


Of course, this is in no way portable, because of the [[ operator. But we can make use of ( ) or . Here's for example how this works in /bin/dash:



$ _has_string()( [ "$1" = "string" ]; )
$ if _has_string "string"; then echo "YES"; fi
YES
$ if _has_string "nope" ; then echo "YES"; fi
$





share|improve this answer

















  • 1




    I wouldn't use the parenthesis in that function definition, they run a subshell which is in no way needed here.
    – ilkkachu
    May 29 at 11:28














up vote
0
down vote













As others already pointed out, what you need to merely use the function within the if statement. Here's something you might not have known:



A function can be treated as a form of compound command. And according to bash manual [[ is one of the operators that can be used in construction of a compound command. Thus, your function _has_string() can be implemented as so:



$ _has_string()[[ "$1" = "string" ]]
$ if _has_string "string"; then echo "YES"; fi
YES


Of course, this is in no way portable, because of the [[ operator. But we can make use of ( ) or . Here's for example how this works in /bin/dash:



$ _has_string()( [ "$1" = "string" ]; )
$ if _has_string "string"; then echo "YES"; fi
YES
$ if _has_string "nope" ; then echo "YES"; fi
$





share|improve this answer

















  • 1




    I wouldn't use the parenthesis in that function definition, they run a subshell which is in no way needed here.
    – ilkkachu
    May 29 at 11:28












up vote
0
down vote










up vote
0
down vote









As others already pointed out, what you need to merely use the function within the if statement. Here's something you might not have known:



A function can be treated as a form of compound command. And according to bash manual [[ is one of the operators that can be used in construction of a compound command. Thus, your function _has_string() can be implemented as so:



$ _has_string()[[ "$1" = "string" ]]
$ if _has_string "string"; then echo "YES"; fi
YES


Of course, this is in no way portable, because of the [[ operator. But we can make use of ( ) or . Here's for example how this works in /bin/dash:



$ _has_string()( [ "$1" = "string" ]; )
$ if _has_string "string"; then echo "YES"; fi
YES
$ if _has_string "nope" ; then echo "YES"; fi
$





share|improve this answer













As others already pointed out, what you need to merely use the function within the if statement. Here's something you might not have known:



A function can be treated as a form of compound command. And according to bash manual [[ is one of the operators that can be used in construction of a compound command. Thus, your function _has_string() can be implemented as so:



$ _has_string()[[ "$1" = "string" ]]
$ if _has_string "string"; then echo "YES"; fi
YES


Of course, this is in no way portable, because of the [[ operator. But we can make use of ( ) or . Here's for example how this works in /bin/dash:



$ _has_string()( [ "$1" = "string" ]; )
$ if _has_string "string"; then echo "YES"; fi
YES
$ if _has_string "nope" ; then echo "YES"; fi
$






share|improve this answer













share|improve this answer



share|improve this answer











answered May 28 at 5:47









Sergiy Kolodyazhnyy

7,56611545




7,56611545







  • 1




    I wouldn't use the parenthesis in that function definition, they run a subshell which is in no way needed here.
    – ilkkachu
    May 29 at 11:28












  • 1




    I wouldn't use the parenthesis in that function definition, they run a subshell which is in no way needed here.
    – ilkkachu
    May 29 at 11:28







1




1




I wouldn't use the parenthesis in that function definition, they run a subshell which is in no way needed here.
– ilkkachu
May 29 at 11:28




I wouldn't use the parenthesis in that function definition, they run a subshell which is in no way needed here.
– ilkkachu
May 29 at 11:28












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f446339%2fbash-assign-variable-result-of-bool-function-then-check%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?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?