How can I prevent unsupported 'shopt' options from causing errors in my .bashrc?
Clash Royale CLAN TAG#URR8PPP
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
add a comment |
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
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
add a comment |
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
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
bash shell-script configuration shopt
edited Feb 5 at 17:26
TheDudeAbides
asked Feb 5 at 17:18
TheDudeAbidesTheDudeAbides
1706
1706
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
add a comment |
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
How my answer did not help?
– Luciano Andress Martini
Feb 5 at 17:33
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
@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
Look in my answer i have something about comparing programs version with shell script.
– Luciano Andress Martini
Feb 7 at 17:16
add a comment |
3 Answers
3
active
oldest
votes
Check if direxpand
is present in the output of shopt
and enable it if it is:
shopt | grep -q '^direxpandb' && shopt -s direxpand
4
Better make thatgrep -q '^direxpandb'
in case some future version or fork of bash has an option that contains this as a substring and removesdirexpand
. 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 includeset -e
at the top, so I tend to use short-cut logic this way around.
– David Foerster
Feb 7 at 18:07
add a comment |
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 thecompgen
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 whatcompgen -A shopt -X ...
even meant.
– TheDudeAbides
Feb 5 at 18:21
3
@TheDudeAbides I read about usingcompgen
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
add a comment |
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?
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Check if direxpand
is present in the output of shopt
and enable it if it is:
shopt | grep -q '^direxpandb' && shopt -s direxpand
4
Better make thatgrep -q '^direxpandb'
in case some future version or fork of bash has an option that contains this as a substring and removesdirexpand
. 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 includeset -e
at the top, so I tend to use short-cut logic this way around.
– David Foerster
Feb 7 at 18:07
add a comment |
Check if direxpand
is present in the output of shopt
and enable it if it is:
shopt | grep -q '^direxpandb' && shopt -s direxpand
4
Better make thatgrep -q '^direxpandb'
in case some future version or fork of bash has an option that contains this as a substring and removesdirexpand
. 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 includeset -e
at the top, so I tend to use short-cut logic this way around.
– David Foerster
Feb 7 at 18:07
add a comment |
Check if direxpand
is present in the output of shopt
and enable it if it is:
shopt | grep -q '^direxpandb' && shopt -s direxpand
Check if direxpand
is present in the output of shopt
and enable it if it is:
shopt | grep -q '^direxpandb' && shopt -s direxpand
edited Feb 5 at 18:12
TheDudeAbides
1706
1706
answered Feb 5 at 17:23
Luciano Andress MartiniLuciano Andress Martini
3,923935
3,923935
4
Better make thatgrep -q '^direxpandb'
in case some future version or fork of bash has an option that contains this as a substring and removesdirexpand
. 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 includeset -e
at the top, so I tend to use short-cut logic this way around.
– David Foerster
Feb 7 at 18:07
add a comment |
4
Better make thatgrep -q '^direxpandb'
in case some future version or fork of bash has an option that contains this as a substring and removesdirexpand
. 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 includeset -e
at the top, so I tend to use short-cut logic this way around.
– David Foerster
Feb 7 at 18:07
4
4
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
Feb 5 at 17:45
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
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
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
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
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
@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
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
@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
add a comment |
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 thecompgen
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 whatcompgen -A shopt -X ...
even meant.
– TheDudeAbides
Feb 5 at 18:21
3
@TheDudeAbides I read about usingcompgen
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
add a comment |
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 thecompgen
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 whatcompgen -A shopt -X ...
even meant.
– TheDudeAbides
Feb 5 at 18:21
3
@TheDudeAbides I read about usingcompgen
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
add a comment |
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.
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.
answered Feb 5 at 17:44
GillesGilles
539k12810911606
539k12810911606
That is not where my brain would've gone, but I like thecompgen
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 whatcompgen -A shopt -X ...
even meant.
– TheDudeAbides
Feb 5 at 18:21
3
@TheDudeAbides I read about usingcompgen
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
add a comment |
That is not where my brain would've gone, but I like thecompgen
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 whatcompgen -A shopt -X ...
even meant.
– TheDudeAbides
Feb 5 at 18:21
3
@TheDudeAbides I read about usingcompgen
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
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
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
+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
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
@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
add a comment |
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?
add a comment |
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?
add a comment |
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?
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?
edited Feb 5 at 18:03
answered Feb 5 at 17:18
TheDudeAbidesTheDudeAbides
1706
1706
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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