Print condition result directly in bash without using if












2















Let's say I have this simple code:



echo "Are there any arguments?"
if [ $# -eq 0 ]; then
echo "false"
else
echo "true"
fi


As you can see it would be better to just have opportunity to directly print condition result, but I don't know how to do it.



It would be something like:



echo "$([ $# -eq 0 ])"



But it doesn't work that way. Can we do this without if?










share|improve this question

























  • You can try zsh. Zsh will change PS1 color (command prompt color) according to $?(the return value of the last command). imgur.com/a/yBz2H

    – Weekend
    Aug 10 '17 at 16:04
















2















Let's say I have this simple code:



echo "Are there any arguments?"
if [ $# -eq 0 ]; then
echo "false"
else
echo "true"
fi


As you can see it would be better to just have opportunity to directly print condition result, but I don't know how to do it.



It would be something like:



echo "$([ $# -eq 0 ])"



But it doesn't work that way. Can we do this without if?










share|improve this question

























  • You can try zsh. Zsh will change PS1 color (command prompt color) according to $?(the return value of the last command). imgur.com/a/yBz2H

    – Weekend
    Aug 10 '17 at 16:04














2












2








2


1






Let's say I have this simple code:



echo "Are there any arguments?"
if [ $# -eq 0 ]; then
echo "false"
else
echo "true"
fi


As you can see it would be better to just have opportunity to directly print condition result, but I don't know how to do it.



It would be something like:



echo "$([ $# -eq 0 ])"



But it doesn't work that way. Can we do this without if?










share|improve this question
















Let's say I have this simple code:



echo "Are there any arguments?"
if [ $# -eq 0 ]; then
echo "false"
else
echo "true"
fi


As you can see it would be better to just have opportunity to directly print condition result, but I don't know how to do it.



It would be something like:



echo "$([ $# -eq 0 ])"



But it doesn't work that way. Can we do this without if?







bash shell test






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 15 '15 at 22:50









Gilles

538k12810891606




538k12810891606










asked Oct 15 '15 at 19:34









ctomekctomek

12015




12015













  • You can try zsh. Zsh will change PS1 color (command prompt color) according to $?(the return value of the last command). imgur.com/a/yBz2H

    – Weekend
    Aug 10 '17 at 16:04



















  • You can try zsh. Zsh will change PS1 color (command prompt color) according to $?(the return value of the last command). imgur.com/a/yBz2H

    – Weekend
    Aug 10 '17 at 16:04

















You can try zsh. Zsh will change PS1 color (command prompt color) according to $?(the return value of the last command). imgur.com/a/yBz2H

– Weekend
Aug 10 '17 at 16:04





You can try zsh. Zsh will change PS1 color (command prompt color) according to $?(the return value of the last command). imgur.com/a/yBz2H

– Weekend
Aug 10 '17 at 16:04










3 Answers
3






active

oldest

votes


















4














You can use the list control operators && and || instead:



[[ $# -eq 0 ]] && { echo false; } || { echo true; }


The { } group a list of commands, you don't need them for just a single command, but they often make such constructions more readable.






share|improve this answer































    2














    You can use $? that keeps the exit code of the last executed command:



    echo "Are there any arguments?"
    [ $# -eq 0 ]
    echo $?





    share|improve this answer































      0














      Since I can't post comments yet (insufficient rep), I'll point out in a new answer that mr-spuratic's solution also works with single brackets, like so:



      [ $# -eq 0 ] && { echo false; } || { echo true; }




      share























        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',
        autoActivateHeartbeat: false,
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        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%2f236487%2fprint-condition-result-directly-in-bash-without-using-if%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        4














        You can use the list control operators && and || instead:



        [[ $# -eq 0 ]] && { echo false; } || { echo true; }


        The { } group a list of commands, you don't need them for just a single command, but they often make such constructions more readable.






        share|improve this answer




























          4














          You can use the list control operators && and || instead:



          [[ $# -eq 0 ]] && { echo false; } || { echo true; }


          The { } group a list of commands, you don't need them for just a single command, but they often make such constructions more readable.






          share|improve this answer


























            4












            4








            4







            You can use the list control operators && and || instead:



            [[ $# -eq 0 ]] && { echo false; } || { echo true; }


            The { } group a list of commands, you don't need them for just a single command, but they often make such constructions more readable.






            share|improve this answer













            You can use the list control operators && and || instead:



            [[ $# -eq 0 ]] && { echo false; } || { echo true; }


            The { } group a list of commands, you don't need them for just a single command, but they often make such constructions more readable.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 15 '15 at 19:46









            mr.spuraticmr.spuratic

            6,9811128




            6,9811128

























                2














                You can use $? that keeps the exit code of the last executed command:



                echo "Are there any arguments?"
                [ $# -eq 0 ]
                echo $?





                share|improve this answer




























                  2














                  You can use $? that keeps the exit code of the last executed command:



                  echo "Are there any arguments?"
                  [ $# -eq 0 ]
                  echo $?





                  share|improve this answer


























                    2












                    2








                    2







                    You can use $? that keeps the exit code of the last executed command:



                    echo "Are there any arguments?"
                    [ $# -eq 0 ]
                    echo $?





                    share|improve this answer













                    You can use $? that keeps the exit code of the last executed command:



                    echo "Are there any arguments?"
                    [ $# -eq 0 ]
                    echo $?






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Oct 15 '15 at 19:46









                    chorobachoroba

                    26.7k44975




                    26.7k44975























                        0














                        Since I can't post comments yet (insufficient rep), I'll point out in a new answer that mr-spuratic's solution also works with single brackets, like so:



                        [ $# -eq 0 ] && { echo false; } || { echo true; }




                        share




























                          0














                          Since I can't post comments yet (insufficient rep), I'll point out in a new answer that mr-spuratic's solution also works with single brackets, like so:



                          [ $# -eq 0 ] && { echo false; } || { echo true; }




                          share


























                            0












                            0








                            0







                            Since I can't post comments yet (insufficient rep), I'll point out in a new answer that mr-spuratic's solution also works with single brackets, like so:



                            [ $# -eq 0 ] && { echo false; } || { echo true; }




                            share













                            Since I can't post comments yet (insufficient rep), I'll point out in a new answer that mr-spuratic's solution also works with single brackets, like so:



                            [ $# -eq 0 ] && { echo false; } || { echo true; }





                            share











                            share


                            share










                            answered 4 mins ago









                            MrPotatoHeadMrPotatoHead

                            11




                            11






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f236487%2fprint-condition-result-directly-in-bash-without-using-if%23new-answer', 'question_page');
                                }
                                );

                                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







                                Popular posts from this blog

                                Histoire des bourses de valeurs

                                Why is there Russian traffic in my log files?

                                Rename multiple files to decrement number in file name?