7

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).

  • How my answer did not help? – Luciano Andress Martini Feb 5 at 17:33
  • @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 Feb 5 at 18:28
  • Look in my answer i have something about comparing programs version with shell script. – Luciano Andress Martini Feb 7 at 17:16
12

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

shopt | grep -q '^direxpand\b' && shopt -s direxpand
  • 4
    Better make that grep -q '^direxpand\b' 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 Feb 5 at 17:45
  • 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 Feb 5 at 18:05
  • 4
    Bash allows to query specific shell options, so one can use [ -z "$(shopt -po direxpand 2>&-)" ] || shopt -s direxpand. No more regex issues! :-) – David Foerster Feb 5 at 21:47
  • @DavidFoerster I'd turn the logic around: [ -n "blah" ] && shopt blah The way you phrase it, you're saying "if direxpand is unsupported, then don't do this thing". – Rich Feb 6 at 16:47
  • 1
    @Rich: Most of my shell scripts include set -e at the top, so I tend to use short-cut logic this way around. – David Foerster Feb 7 at 18:07
14

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.

  • 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 Feb 5 at 18:09
  • +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 Feb 5 at 18:21
  • 3
    @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 Feb 5 at 18:29
3

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.