How can I prevent unsupported 'shopt' options from causing errors in my .bashrc?












2















I work in a relatively heterogeneous environment where I may be running different versions of Bash on different HPC nodes, VMs, or my personal workstation.



I like the default behavior of Bash <= 4.1 that expands cd $SOMEPATH into cd /the/actual/path when pressing the Tab key. This is just one example, though; complete_fullquote (though I don't know exactly what it does) may have also changed default behavior at v4.2.



However, the shopt option, direxpand which switches on this non-default behavior in Bash 4.2 and above is not recognized by earlier versions, and results in an error message being printed to the console every time I log in to a node with an older Bash:



-bash: shopt: direxpand: invalid shell option name


What I'd like to do is wrap a conditional around shop -s direxpand to enable that option on Bash > 4.1 in a robust way (i.e., not just redirecting the error output to /dev/null).










share|improve this question









New contributor




TheDudeAbides is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • How my answer did not help?

    – Luciano Andress Martini
    3 hours ago











  • @LucianoAndressMartini It did, and that's the solution I ended up going with in my own .bashrc. I still wanted a record of how to use $BASH_VERSINFO to interrogate the major/minor version of the running shell, for my own edification, which is why I finished posting my own answer. :)

    – TheDudeAbides
    2 hours ago


















2















I work in a relatively heterogeneous environment where I may be running different versions of Bash on different HPC nodes, VMs, or my personal workstation.



I like the default behavior of Bash <= 4.1 that expands cd $SOMEPATH into cd /the/actual/path when pressing the Tab key. This is just one example, though; complete_fullquote (though I don't know exactly what it does) may have also changed default behavior at v4.2.



However, the shopt option, direxpand which switches on this non-default behavior in Bash 4.2 and above is not recognized by earlier versions, and results in an error message being printed to the console every time I log in to a node with an older Bash:



-bash: shopt: direxpand: invalid shell option name


What I'd like to do is wrap a conditional around shop -s direxpand to enable that option on Bash > 4.1 in a robust way (i.e., not just redirecting the error output to /dev/null).










share|improve this question









New contributor




TheDudeAbides is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • How my answer did not help?

    – Luciano Andress Martini
    3 hours ago











  • @LucianoAndressMartini It did, and that's the solution I ended up going with in my own .bashrc. I still wanted a record of how to use $BASH_VERSINFO to interrogate the major/minor version of the running shell, for my own edification, which is why I finished posting my own answer. :)

    – TheDudeAbides
    2 hours ago
















2












2








2


1






I work in a relatively heterogeneous environment where I may be running different versions of Bash on different HPC nodes, VMs, or my personal workstation.



I like the default behavior of Bash <= 4.1 that expands cd $SOMEPATH into cd /the/actual/path when pressing the Tab key. This is just one example, though; complete_fullquote (though I don't know exactly what it does) may have also changed default behavior at v4.2.



However, the shopt option, direxpand which switches on this non-default behavior in Bash 4.2 and above is not recognized by earlier versions, and results in an error message being printed to the console every time I log in to a node with an older Bash:



-bash: shopt: direxpand: invalid shell option name


What I'd like to do is wrap a conditional around shop -s direxpand to enable that option on Bash > 4.1 in a robust way (i.e., not just redirecting the error output to /dev/null).










share|improve this question









New contributor




TheDudeAbides is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I work in a relatively heterogeneous environment where I may be running different versions of Bash on different HPC nodes, VMs, or my personal workstation.



I like the default behavior of Bash <= 4.1 that expands cd $SOMEPATH into cd /the/actual/path when pressing the Tab key. This is just one example, though; complete_fullquote (though I don't know exactly what it does) may have also changed default behavior at v4.2.



However, the shopt option, direxpand which switches on this non-default behavior in Bash 4.2 and above is not recognized by earlier versions, and results in an error message being printed to the console every time I log in to a node with an older Bash:



-bash: shopt: direxpand: invalid shell option name


What I'd like to do is wrap a conditional around shop -s direxpand to enable that option on Bash > 4.1 in a robust way (i.e., not just redirecting the error output to /dev/null).







bash shell-script configuration shopt






share|improve this question









New contributor




TheDudeAbides is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




TheDudeAbides is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 3 hours ago







TheDudeAbides













New contributor




TheDudeAbides is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 3 hours ago









TheDudeAbidesTheDudeAbides

1255




1255




New contributor




TheDudeAbides is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





TheDudeAbides is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






TheDudeAbides is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • How my answer did not help?

    – Luciano Andress Martini
    3 hours ago











  • @LucianoAndressMartini It did, and that's the solution I ended up going with in my own .bashrc. I still wanted a record of how to use $BASH_VERSINFO to interrogate the major/minor version of the running shell, for my own edification, which is why I finished posting my own answer. :)

    – TheDudeAbides
    2 hours ago





















  • How my answer did not help?

    – Luciano Andress Martini
    3 hours ago











  • @LucianoAndressMartini It did, and that's the solution I ended up going with in my own .bashrc. I still wanted a record of how to use $BASH_VERSINFO to interrogate the major/minor version of the running shell, for my own edification, which is why I finished posting my own answer. :)

    – TheDudeAbides
    2 hours ago



















How my answer did not help?

– Luciano Andress Martini
3 hours ago





How my answer did not help?

– Luciano Andress Martini
3 hours ago













@LucianoAndressMartini It did, and that's the solution I ended up going with in my own .bashrc. I still wanted a record of how to use $BASH_VERSINFO to interrogate the major/minor version of the running shell, for my own edification, which is why I finished posting my own answer. :)

– TheDudeAbides
2 hours ago







@LucianoAndressMartini It did, and that's the solution I ended up going with in my own .bashrc. I still wanted a record of how to use $BASH_VERSINFO to interrogate the major/minor version of the running shell, for my own edification, which is why I finished posting my own answer. :)

– TheDudeAbides
2 hours ago












3 Answers
3






active

oldest

votes


















5














Check if direxpand is present in the output of shopt and enable it if it is:



shopt | grep -q '^direxpandb' && shopt -s direxpand





share|improve this answer





















  • 2





    Better make that grep -q '^direxpandb' in case some future version or fork of bash has an option that contains this as a substring and removes direxpand. Unlikely in this specific case, but it doesn't cost much to be robust.

    – Gilles
    3 hours ago











  • Thanks Luciano. I'd intended to answer my own question, but I"ll accept your answer after my edits go through peer review. Maybe you can approve them yourself?

    – TheDudeAbides
    2 hours ago



















6














I don't see what's wrong with redirecting errors to /dev/null. If you want your code to be robust to set -e, use the common idiom … || true:



shopt -s direxpand 2>/dev/null || true


If you want to run some fallback code if the option does not exist, use the return status of shopt:



if shopt -s direxpand 2>/dev/null; then
… # the direxpand option exists
else
… # the direxpand option does not exist
fi


But if you really dislike redirecting the error away, you can use the completion mechanism to perform introspection. This assumes that you don't have antiquated machines with bash ≤ 2.03 that didn't have programmable completion.



shopt_exists () {
compgen -A shopt -X !"$1" "$1" >/dev/null
}
if shopt_exists direxpand; then
shopt -s direxpand
fi


This method avoids forking, which is slow on some environments such as Cygwin. So does the straightforward 2>/dev/null, I don't think you can beat that on performance.






share|improve this answer
























  • That is not where my brain would've gone, but I like the compgen proposal. That's varsity level stuff right there! Avoiding redirection to /dev/null is just a personal preference. I like to ask for permission instead of forgiveness, if that makes sense? :)

    – TheDudeAbides
    2 hours ago













  • +1 for a totally unanticipated schooling in Bash programmable completion, which forced me to go to the manual to decipher what compgen -A shopt -X ... even meant.

    – TheDudeAbides
    2 hours ago








  • 2





    @TheDudeAbides I read about using compgen this way on Unix & Linux, I don't know who first proposed it. (I stopped using bash as my main shell before it had programmable completion.) In programming it's usually a bad idea to ask for permission because there's a risk that the permission check will not match what you're actually doing, either because of a coding error (where you aren't quite checking what you think you're checking) or because what you checked changed before you used it.

    – Gilles
    2 hours ago



















1














I found most of a solution in this SE thread.



When you know for sure that a specific shopt option is available at a certain major/minor release of Bash, here's one way to enable it conditionally:



if [[ ${BASH_VERSINFO[0]} -eq 4 && ${BASH_VERSINFO[1]} -gt 1 ]]; then
shopt -s direxpand
fi


Obviously, the conditional expression gets a little more complicated when Bash reaches version 5. Note the braces around BASH_VERINFO[index], which are required, and the use of -eq and -gt, which do integer rather than (locale-dependent) lexical comparisons.



The $BASH_VERSINFO array contains all the information you'd see in the output of bash --version:



bash --version | head -1
# result:
# GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)

declare -p BASH_VERSINFO
# result:
# declare -ar BASH_VERSINFO='([0]="4" [1]="3" [2]="48" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu")'


When it isn't clear from the documentation for shopt at which Bash version(s) became supported or changed their behavior, the method proposed by Luciano is probably fine:



# note the '-q' so that the matched pattern isn't actually printed
shopt | grep -q direxpand && shopt -s direxpand


References: 1, 2, 3

Related reading: Set and Shopt - Why Two?






share|improve this answer










New contributor




TheDudeAbides is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















    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
    });


    }
    });






    TheDudeAbides is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f498857%2fhow-can-i-prevent-unsupported-shopt-options-from-causing-errors-in-my-bashrc%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









    5














    Check if direxpand is present in the output of shopt and enable it if it is:



    shopt | grep -q '^direxpandb' && shopt -s direxpand





    share|improve this answer





















    • 2





      Better make that grep -q '^direxpandb' in case some future version or fork of bash has an option that contains this as a substring and removes direxpand. Unlikely in this specific case, but it doesn't cost much to be robust.

      – Gilles
      3 hours ago











    • Thanks Luciano. I'd intended to answer my own question, but I"ll accept your answer after my edits go through peer review. Maybe you can approve them yourself?

      – TheDudeAbides
      2 hours ago
















    5














    Check if direxpand is present in the output of shopt and enable it if it is:



    shopt | grep -q '^direxpandb' && shopt -s direxpand





    share|improve this answer





















    • 2





      Better make that grep -q '^direxpandb' in case some future version or fork of bash has an option that contains this as a substring and removes direxpand. Unlikely in this specific case, but it doesn't cost much to be robust.

      – Gilles
      3 hours ago











    • Thanks Luciano. I'd intended to answer my own question, but I"ll accept your answer after my edits go through peer review. Maybe you can approve them yourself?

      – TheDudeAbides
      2 hours ago














    5












    5








    5







    Check if direxpand is present in the output of shopt and enable it if it is:



    shopt | grep -q '^direxpandb' && shopt -s direxpand





    share|improve this answer















    Check if direxpand is present in the output of shopt and enable it if it is:



    shopt | grep -q '^direxpandb' && shopt -s direxpand






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 2 hours ago









    TheDudeAbides

    1255




    1255










    answered 3 hours ago









    Luciano Andress MartiniLuciano Andress Martini

    3,803933




    3,803933








    • 2





      Better make that grep -q '^direxpandb' in case some future version or fork of bash has an option that contains this as a substring and removes direxpand. Unlikely in this specific case, but it doesn't cost much to be robust.

      – Gilles
      3 hours ago











    • Thanks Luciano. I'd intended to answer my own question, but I"ll accept your answer after my edits go through peer review. Maybe you can approve them yourself?

      – TheDudeAbides
      2 hours ago














    • 2





      Better make that grep -q '^direxpandb' in case some future version or fork of bash has an option that contains this as a substring and removes direxpand. Unlikely in this specific case, but it doesn't cost much to be robust.

      – Gilles
      3 hours ago











    • Thanks Luciano. I'd intended to answer my own question, but I"ll accept your answer after my edits go through peer review. Maybe you can approve them yourself?

      – TheDudeAbides
      2 hours ago








    2




    2





    Better make that grep -q '^direxpandb' in case some future version or fork of bash has an option that contains this as a substring and removes direxpand. Unlikely in this specific case, but it doesn't cost much to be robust.

    – Gilles
    3 hours ago





    Better make that grep -q '^direxpandb' in case some future version or fork of bash has an option that contains this as a substring and removes direxpand. Unlikely in this specific case, but it doesn't cost much to be robust.

    – Gilles
    3 hours ago













    Thanks Luciano. I'd intended to answer my own question, but I"ll accept your answer after my edits go through peer review. Maybe you can approve them yourself?

    – TheDudeAbides
    2 hours ago





    Thanks Luciano. I'd intended to answer my own question, but I"ll accept your answer after my edits go through peer review. Maybe you can approve them yourself?

    – TheDudeAbides
    2 hours ago













    6














    I don't see what's wrong with redirecting errors to /dev/null. If you want your code to be robust to set -e, use the common idiom … || true:



    shopt -s direxpand 2>/dev/null || true


    If you want to run some fallback code if the option does not exist, use the return status of shopt:



    if shopt -s direxpand 2>/dev/null; then
    … # the direxpand option exists
    else
    … # the direxpand option does not exist
    fi


    But if you really dislike redirecting the error away, you can use the completion mechanism to perform introspection. This assumes that you don't have antiquated machines with bash ≤ 2.03 that didn't have programmable completion.



    shopt_exists () {
    compgen -A shopt -X !"$1" "$1" >/dev/null
    }
    if shopt_exists direxpand; then
    shopt -s direxpand
    fi


    This method avoids forking, which is slow on some environments such as Cygwin. So does the straightforward 2>/dev/null, I don't think you can beat that on performance.






    share|improve this answer
























    • That is not where my brain would've gone, but I like the compgen proposal. That's varsity level stuff right there! Avoiding redirection to /dev/null is just a personal preference. I like to ask for permission instead of forgiveness, if that makes sense? :)

      – TheDudeAbides
      2 hours ago













    • +1 for a totally unanticipated schooling in Bash programmable completion, which forced me to go to the manual to decipher what compgen -A shopt -X ... even meant.

      – TheDudeAbides
      2 hours ago








    • 2





      @TheDudeAbides I read about using compgen this way on Unix & Linux, I don't know who first proposed it. (I stopped using bash as my main shell before it had programmable completion.) In programming it's usually a bad idea to ask for permission because there's a risk that the permission check will not match what you're actually doing, either because of a coding error (where you aren't quite checking what you think you're checking) or because what you checked changed before you used it.

      – Gilles
      2 hours ago
















    6














    I don't see what's wrong with redirecting errors to /dev/null. If you want your code to be robust to set -e, use the common idiom … || true:



    shopt -s direxpand 2>/dev/null || true


    If you want to run some fallback code if the option does not exist, use the return status of shopt:



    if shopt -s direxpand 2>/dev/null; then
    … # the direxpand option exists
    else
    … # the direxpand option does not exist
    fi


    But if you really dislike redirecting the error away, you can use the completion mechanism to perform introspection. This assumes that you don't have antiquated machines with bash ≤ 2.03 that didn't have programmable completion.



    shopt_exists () {
    compgen -A shopt -X !"$1" "$1" >/dev/null
    }
    if shopt_exists direxpand; then
    shopt -s direxpand
    fi


    This method avoids forking, which is slow on some environments such as Cygwin. So does the straightforward 2>/dev/null, I don't think you can beat that on performance.






    share|improve this answer
























    • That is not where my brain would've gone, but I like the compgen proposal. That's varsity level stuff right there! Avoiding redirection to /dev/null is just a personal preference. I like to ask for permission instead of forgiveness, if that makes sense? :)

      – TheDudeAbides
      2 hours ago













    • +1 for a totally unanticipated schooling in Bash programmable completion, which forced me to go to the manual to decipher what compgen -A shopt -X ... even meant.

      – TheDudeAbides
      2 hours ago








    • 2





      @TheDudeAbides I read about using compgen this way on Unix & Linux, I don't know who first proposed it. (I stopped using bash as my main shell before it had programmable completion.) In programming it's usually a bad idea to ask for permission because there's a risk that the permission check will not match what you're actually doing, either because of a coding error (where you aren't quite checking what you think you're checking) or because what you checked changed before you used it.

      – Gilles
      2 hours ago














    6












    6








    6







    I don't see what's wrong with redirecting errors to /dev/null. If you want your code to be robust to set -e, use the common idiom … || true:



    shopt -s direxpand 2>/dev/null || true


    If you want to run some fallback code if the option does not exist, use the return status of shopt:



    if shopt -s direxpand 2>/dev/null; then
    … # the direxpand option exists
    else
    … # the direxpand option does not exist
    fi


    But if you really dislike redirecting the error away, you can use the completion mechanism to perform introspection. This assumes that you don't have antiquated machines with bash ≤ 2.03 that didn't have programmable completion.



    shopt_exists () {
    compgen -A shopt -X !"$1" "$1" >/dev/null
    }
    if shopt_exists direxpand; then
    shopt -s direxpand
    fi


    This method avoids forking, which is slow on some environments such as Cygwin. So does the straightforward 2>/dev/null, I don't think you can beat that on performance.






    share|improve this answer













    I don't see what's wrong with redirecting errors to /dev/null. If you want your code to be robust to set -e, use the common idiom … || true:



    shopt -s direxpand 2>/dev/null || true


    If you want to run some fallback code if the option does not exist, use the return status of shopt:



    if shopt -s direxpand 2>/dev/null; then
    … # the direxpand option exists
    else
    … # the direxpand option does not exist
    fi


    But if you really dislike redirecting the error away, you can use the completion mechanism to perform introspection. This assumes that you don't have antiquated machines with bash ≤ 2.03 that didn't have programmable completion.



    shopt_exists () {
    compgen -A shopt -X !"$1" "$1" >/dev/null
    }
    if shopt_exists direxpand; then
    shopt -s direxpand
    fi


    This method avoids forking, which is slow on some environments such as Cygwin. So does the straightforward 2>/dev/null, I don't think you can beat that on performance.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 3 hours ago









    GillesGilles

    535k12810811598




    535k12810811598













    • That is not where my brain would've gone, but I like the compgen proposal. That's varsity level stuff right there! Avoiding redirection to /dev/null is just a personal preference. I like to ask for permission instead of forgiveness, if that makes sense? :)

      – TheDudeAbides
      2 hours ago













    • +1 for a totally unanticipated schooling in Bash programmable completion, which forced me to go to the manual to decipher what compgen -A shopt -X ... even meant.

      – TheDudeAbides
      2 hours ago








    • 2





      @TheDudeAbides I read about using compgen this way on Unix & Linux, I don't know who first proposed it. (I stopped using bash as my main shell before it had programmable completion.) In programming it's usually a bad idea to ask for permission because there's a risk that the permission check will not match what you're actually doing, either because of a coding error (where you aren't quite checking what you think you're checking) or because what you checked changed before you used it.

      – Gilles
      2 hours ago



















    • That is not where my brain would've gone, but I like the compgen proposal. That's varsity level stuff right there! Avoiding redirection to /dev/null is just a personal preference. I like to ask for permission instead of forgiveness, if that makes sense? :)

      – TheDudeAbides
      2 hours ago













    • +1 for a totally unanticipated schooling in Bash programmable completion, which forced me to go to the manual to decipher what compgen -A shopt -X ... even meant.

      – TheDudeAbides
      2 hours ago








    • 2





      @TheDudeAbides I read about using compgen this way on Unix & Linux, I don't know who first proposed it. (I stopped using bash as my main shell before it had programmable completion.) In programming it's usually a bad idea to ask for permission because there's a risk that the permission check will not match what you're actually doing, either because of a coding error (where you aren't quite checking what you think you're checking) or because what you checked changed before you used it.

      – Gilles
      2 hours ago

















    That is not where my brain would've gone, but I like the compgen proposal. That's varsity level stuff right there! Avoiding redirection to /dev/null is just a personal preference. I like to ask for permission instead of forgiveness, if that makes sense? :)

    – TheDudeAbides
    2 hours ago







    That is not where my brain would've gone, but I like the compgen proposal. That's varsity level stuff right there! Avoiding redirection to /dev/null is just a personal preference. I like to ask for permission instead of forgiveness, if that makes sense? :)

    – TheDudeAbides
    2 hours ago















    +1 for a totally unanticipated schooling in Bash programmable completion, which forced me to go to the manual to decipher what compgen -A shopt -X ... even meant.

    – TheDudeAbides
    2 hours ago







    +1 for a totally unanticipated schooling in Bash programmable completion, which forced me to go to the manual to decipher what compgen -A shopt -X ... even meant.

    – TheDudeAbides
    2 hours ago






    2




    2





    @TheDudeAbides I read about using compgen this way on Unix & Linux, I don't know who first proposed it. (I stopped using bash as my main shell before it had programmable completion.) In programming it's usually a bad idea to ask for permission because there's a risk that the permission check will not match what you're actually doing, either because of a coding error (where you aren't quite checking what you think you're checking) or because what you checked changed before you used it.

    – Gilles
    2 hours ago





    @TheDudeAbides I read about using compgen this way on Unix & Linux, I don't know who first proposed it. (I stopped using bash as my main shell before it had programmable completion.) In programming it's usually a bad idea to ask for permission because there's a risk that the permission check will not match what you're actually doing, either because of a coding error (where you aren't quite checking what you think you're checking) or because what you checked changed before you used it.

    – Gilles
    2 hours ago











    1














    I found most of a solution in this SE thread.



    When you know for sure that a specific shopt option is available at a certain major/minor release of Bash, here's one way to enable it conditionally:



    if [[ ${BASH_VERSINFO[0]} -eq 4 && ${BASH_VERSINFO[1]} -gt 1 ]]; then
    shopt -s direxpand
    fi


    Obviously, the conditional expression gets a little more complicated when Bash reaches version 5. Note the braces around BASH_VERINFO[index], which are required, and the use of -eq and -gt, which do integer rather than (locale-dependent) lexical comparisons.



    The $BASH_VERSINFO array contains all the information you'd see in the output of bash --version:



    bash --version | head -1
    # result:
    # GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)

    declare -p BASH_VERSINFO
    # result:
    # declare -ar BASH_VERSINFO='([0]="4" [1]="3" [2]="48" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu")'


    When it isn't clear from the documentation for shopt at which Bash version(s) became supported or changed their behavior, the method proposed by Luciano is probably fine:



    # note the '-q' so that the matched pattern isn't actually printed
    shopt | grep -q direxpand && shopt -s direxpand


    References: 1, 2, 3

    Related reading: Set and Shopt - Why Two?






    share|improve this answer










    New contributor




    TheDudeAbides is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

























      1














      I found most of a solution in this SE thread.



      When you know for sure that a specific shopt option is available at a certain major/minor release of Bash, here's one way to enable it conditionally:



      if [[ ${BASH_VERSINFO[0]} -eq 4 && ${BASH_VERSINFO[1]} -gt 1 ]]; then
      shopt -s direxpand
      fi


      Obviously, the conditional expression gets a little more complicated when Bash reaches version 5. Note the braces around BASH_VERINFO[index], which are required, and the use of -eq and -gt, which do integer rather than (locale-dependent) lexical comparisons.



      The $BASH_VERSINFO array contains all the information you'd see in the output of bash --version:



      bash --version | head -1
      # result:
      # GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)

      declare -p BASH_VERSINFO
      # result:
      # declare -ar BASH_VERSINFO='([0]="4" [1]="3" [2]="48" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu")'


      When it isn't clear from the documentation for shopt at which Bash version(s) became supported or changed their behavior, the method proposed by Luciano is probably fine:



      # note the '-q' so that the matched pattern isn't actually printed
      shopt | grep -q direxpand && shopt -s direxpand


      References: 1, 2, 3

      Related reading: Set and Shopt - Why Two?






      share|improve this answer










      New contributor




      TheDudeAbides is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.























        1












        1








        1







        I found most of a solution in this SE thread.



        When you know for sure that a specific shopt option is available at a certain major/minor release of Bash, here's one way to enable it conditionally:



        if [[ ${BASH_VERSINFO[0]} -eq 4 && ${BASH_VERSINFO[1]} -gt 1 ]]; then
        shopt -s direxpand
        fi


        Obviously, the conditional expression gets a little more complicated when Bash reaches version 5. Note the braces around BASH_VERINFO[index], which are required, and the use of -eq and -gt, which do integer rather than (locale-dependent) lexical comparisons.



        The $BASH_VERSINFO array contains all the information you'd see in the output of bash --version:



        bash --version | head -1
        # result:
        # GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)

        declare -p BASH_VERSINFO
        # result:
        # declare -ar BASH_VERSINFO='([0]="4" [1]="3" [2]="48" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu")'


        When it isn't clear from the documentation for shopt at which Bash version(s) became supported or changed their behavior, the method proposed by Luciano is probably fine:



        # note the '-q' so that the matched pattern isn't actually printed
        shopt | grep -q direxpand && shopt -s direxpand


        References: 1, 2, 3

        Related reading: Set and Shopt - Why Two?






        share|improve this answer










        New contributor




        TheDudeAbides is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.










        I found most of a solution in this SE thread.



        When you know for sure that a specific shopt option is available at a certain major/minor release of Bash, here's one way to enable it conditionally:



        if [[ ${BASH_VERSINFO[0]} -eq 4 && ${BASH_VERSINFO[1]} -gt 1 ]]; then
        shopt -s direxpand
        fi


        Obviously, the conditional expression gets a little more complicated when Bash reaches version 5. Note the braces around BASH_VERINFO[index], which are required, and the use of -eq and -gt, which do integer rather than (locale-dependent) lexical comparisons.



        The $BASH_VERSINFO array contains all the information you'd see in the output of bash --version:



        bash --version | head -1
        # result:
        # GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)

        declare -p BASH_VERSINFO
        # result:
        # declare -ar BASH_VERSINFO='([0]="4" [1]="3" [2]="48" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu")'


        When it isn't clear from the documentation for shopt at which Bash version(s) became supported or changed their behavior, the method proposed by Luciano is probably fine:



        # note the '-q' so that the matched pattern isn't actually printed
        shopt | grep -q direxpand && shopt -s direxpand


        References: 1, 2, 3

        Related reading: Set and Shopt - Why Two?







        share|improve this answer










        New contributor




        TheDudeAbides is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer








        edited 3 hours ago





















        New contributor




        TheDudeAbides is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 3 hours ago









        TheDudeAbidesTheDudeAbides

        1255




        1255




        New contributor




        TheDudeAbides is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        TheDudeAbides is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        TheDudeAbides is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






















            TheDudeAbides is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            TheDudeAbides is a new contributor. Be nice, and check out our Code of Conduct.













            TheDudeAbides is a new contributor. Be nice, and check out our Code of Conduct.












            TheDudeAbides is a new contributor. Be nice, and check out our Code of Conduct.
















            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%2f498857%2fhow-can-i-prevent-unsupported-shopt-options-from-causing-errors-in-my-bashrc%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

            How to label and detect the document text images

            Vallis Paradisi

            Tabula Rosettana