bash - case-insensitive matching of variable
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
This syntax prints "linux" when variable equals "no":
[[ $LINUX_CONF = no ]] && echo "linux"
How would I use regular expressions (or similar) in order to make the comparison case insensitive?
linux bash regular-expression case-sensitivity
add a comment |Â
up vote
3
down vote
favorite
This syntax prints "linux" when variable equals "no":
[[ $LINUX_CONF = no ]] && echo "linux"
How would I use regular expressions (or similar) in order to make the comparison case insensitive?
linux bash regular-expression case-sensitivity
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
This syntax prints "linux" when variable equals "no":
[[ $LINUX_CONF = no ]] && echo "linux"
How would I use regular expressions (or similar) in order to make the comparison case insensitive?
linux bash regular-expression case-sensitivity
This syntax prints "linux" when variable equals "no":
[[ $LINUX_CONF = no ]] && echo "linux"
How would I use regular expressions (or similar) in order to make the comparison case insensitive?
linux bash regular-expression case-sensitivity
linux bash regular-expression case-sensitivity
edited Oct 2 '17 at 20:14
nath
633421
633421
asked Oct 2 '17 at 16:09
yael
2,0361145
2,0361145
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
8
down vote
accepted
No need to use that ksh
-style [[...]]
command, you can use the standard sh
case
construct here:
case $LINUX_CONF in
([Nn][Oo]) echo linux;;
(*) echo not linux;;
esac
Or naming each possible case individually:
case $LINUX_CONF in
(No | nO | NO | no) echo linux;;
(*) echo not linux;;
esac
For a bash
-specific way to do case-insensitive matching, you can do:
shopt -s nocasematch
[[ $LINUX_CONF = no ]] && echo linux
Or:
[[ $LINUX_CONF,, = no ]] && echo linux
(where $VAR,,
is the syntax to convert a string to lower case).
You can also force a variable to be converted to lowercase upon assignment with:
typeset -l LINUX_CONF
That also comes from ksh and is also supported by bash
and zsh
.
More variants with other shells:
zsh
set -o nocasematch
[[ $LINUX_CONF =~ no ]] && echo linux
(same as in bash
).
setopt extendedglob
[[ $LINUX_CONF = (#i)no ]] && echo linux
(less dangerous than making all matches case insensitive)
[[ $(L)LINUX_CONF = no ]] && echo linux
(convert to lowercase operator)
set -o rematchpcre
[[ $LINUX_CONF =~ (?i)no ]]
(PCRE syntax)
ksh93
[[ $LINUX_CONF = ~(i)no ]]
or
[[ $LINUX_CONF = ~(i:no) ]]
Note that all approaches above other than [nN][oO]
to do case insensitive matching depend on the user's locale. Not all people around the world agree on what the uppercase version of a given letter is, even for ASCII ones.
In practice for the ASCII ones, at least on GNU systems, the deviations from the English rules seem to be limited to the i
and I
letters and whether the dot is there or not on the uppercase or lowercase version.
What that means is that [[ $VAR,, = oui ]]
is not guaranteed to match on OUI
in every locale (even when the bug in current versions of bash
is fixed).
ok fine but how we do the same in if syntax?
â yael
Oct 2 '17 at 16:13
@yael, why would you want to useif
? If that's for theelse
part, see edit.
â Stéphane Chazelas
Oct 2 '17 at 16:15
There is also[[ "$LINUX_CONF" =~ [Nn][Oo] ]]
(what with the OP specifically asking for a RE).
â DopeGhoti
Oct 2 '17 at 16:21
1
@DopeGhoti, or[[ $LINUX_CONF = [Nn][Oo] ]]
but that has little advantage over the standardcase
syntax.
â Stéphane Chazelas
Oct 2 '17 at 16:22
In this case, I agree for such a simple comparison. I, as you did, would simply squash to lower case wtih$var,,
.
â DopeGhoti
Oct 2 '17 at 16:23
add a comment |Â
up vote
0
down vote
Keep your existing command but on the line before it run this:
LINUX_CONF=$(echo $LINUX_CONF | awk ' print tolower($0) ')
Regardless of the case of the value stored in your variable this will force the replacement value to be lowercase. This results in matching your existing command with only one additional line of code.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
accepted
No need to use that ksh
-style [[...]]
command, you can use the standard sh
case
construct here:
case $LINUX_CONF in
([Nn][Oo]) echo linux;;
(*) echo not linux;;
esac
Or naming each possible case individually:
case $LINUX_CONF in
(No | nO | NO | no) echo linux;;
(*) echo not linux;;
esac
For a bash
-specific way to do case-insensitive matching, you can do:
shopt -s nocasematch
[[ $LINUX_CONF = no ]] && echo linux
Or:
[[ $LINUX_CONF,, = no ]] && echo linux
(where $VAR,,
is the syntax to convert a string to lower case).
You can also force a variable to be converted to lowercase upon assignment with:
typeset -l LINUX_CONF
That also comes from ksh and is also supported by bash
and zsh
.
More variants with other shells:
zsh
set -o nocasematch
[[ $LINUX_CONF =~ no ]] && echo linux
(same as in bash
).
setopt extendedglob
[[ $LINUX_CONF = (#i)no ]] && echo linux
(less dangerous than making all matches case insensitive)
[[ $(L)LINUX_CONF = no ]] && echo linux
(convert to lowercase operator)
set -o rematchpcre
[[ $LINUX_CONF =~ (?i)no ]]
(PCRE syntax)
ksh93
[[ $LINUX_CONF = ~(i)no ]]
or
[[ $LINUX_CONF = ~(i:no) ]]
Note that all approaches above other than [nN][oO]
to do case insensitive matching depend on the user's locale. Not all people around the world agree on what the uppercase version of a given letter is, even for ASCII ones.
In practice for the ASCII ones, at least on GNU systems, the deviations from the English rules seem to be limited to the i
and I
letters and whether the dot is there or not on the uppercase or lowercase version.
What that means is that [[ $VAR,, = oui ]]
is not guaranteed to match on OUI
in every locale (even when the bug in current versions of bash
is fixed).
ok fine but how we do the same in if syntax?
â yael
Oct 2 '17 at 16:13
@yael, why would you want to useif
? If that's for theelse
part, see edit.
â Stéphane Chazelas
Oct 2 '17 at 16:15
There is also[[ "$LINUX_CONF" =~ [Nn][Oo] ]]
(what with the OP specifically asking for a RE).
â DopeGhoti
Oct 2 '17 at 16:21
1
@DopeGhoti, or[[ $LINUX_CONF = [Nn][Oo] ]]
but that has little advantage over the standardcase
syntax.
â Stéphane Chazelas
Oct 2 '17 at 16:22
In this case, I agree for such a simple comparison. I, as you did, would simply squash to lower case wtih$var,,
.
â DopeGhoti
Oct 2 '17 at 16:23
add a comment |Â
up vote
8
down vote
accepted
No need to use that ksh
-style [[...]]
command, you can use the standard sh
case
construct here:
case $LINUX_CONF in
([Nn][Oo]) echo linux;;
(*) echo not linux;;
esac
Or naming each possible case individually:
case $LINUX_CONF in
(No | nO | NO | no) echo linux;;
(*) echo not linux;;
esac
For a bash
-specific way to do case-insensitive matching, you can do:
shopt -s nocasematch
[[ $LINUX_CONF = no ]] && echo linux
Or:
[[ $LINUX_CONF,, = no ]] && echo linux
(where $VAR,,
is the syntax to convert a string to lower case).
You can also force a variable to be converted to lowercase upon assignment with:
typeset -l LINUX_CONF
That also comes from ksh and is also supported by bash
and zsh
.
More variants with other shells:
zsh
set -o nocasematch
[[ $LINUX_CONF =~ no ]] && echo linux
(same as in bash
).
setopt extendedglob
[[ $LINUX_CONF = (#i)no ]] && echo linux
(less dangerous than making all matches case insensitive)
[[ $(L)LINUX_CONF = no ]] && echo linux
(convert to lowercase operator)
set -o rematchpcre
[[ $LINUX_CONF =~ (?i)no ]]
(PCRE syntax)
ksh93
[[ $LINUX_CONF = ~(i)no ]]
or
[[ $LINUX_CONF = ~(i:no) ]]
Note that all approaches above other than [nN][oO]
to do case insensitive matching depend on the user's locale. Not all people around the world agree on what the uppercase version of a given letter is, even for ASCII ones.
In practice for the ASCII ones, at least on GNU systems, the deviations from the English rules seem to be limited to the i
and I
letters and whether the dot is there or not on the uppercase or lowercase version.
What that means is that [[ $VAR,, = oui ]]
is not guaranteed to match on OUI
in every locale (even when the bug in current versions of bash
is fixed).
ok fine but how we do the same in if syntax?
â yael
Oct 2 '17 at 16:13
@yael, why would you want to useif
? If that's for theelse
part, see edit.
â Stéphane Chazelas
Oct 2 '17 at 16:15
There is also[[ "$LINUX_CONF" =~ [Nn][Oo] ]]
(what with the OP specifically asking for a RE).
â DopeGhoti
Oct 2 '17 at 16:21
1
@DopeGhoti, or[[ $LINUX_CONF = [Nn][Oo] ]]
but that has little advantage over the standardcase
syntax.
â Stéphane Chazelas
Oct 2 '17 at 16:22
In this case, I agree for such a simple comparison. I, as you did, would simply squash to lower case wtih$var,,
.
â DopeGhoti
Oct 2 '17 at 16:23
add a comment |Â
up vote
8
down vote
accepted
up vote
8
down vote
accepted
No need to use that ksh
-style [[...]]
command, you can use the standard sh
case
construct here:
case $LINUX_CONF in
([Nn][Oo]) echo linux;;
(*) echo not linux;;
esac
Or naming each possible case individually:
case $LINUX_CONF in
(No | nO | NO | no) echo linux;;
(*) echo not linux;;
esac
For a bash
-specific way to do case-insensitive matching, you can do:
shopt -s nocasematch
[[ $LINUX_CONF = no ]] && echo linux
Or:
[[ $LINUX_CONF,, = no ]] && echo linux
(where $VAR,,
is the syntax to convert a string to lower case).
You can also force a variable to be converted to lowercase upon assignment with:
typeset -l LINUX_CONF
That also comes from ksh and is also supported by bash
and zsh
.
More variants with other shells:
zsh
set -o nocasematch
[[ $LINUX_CONF =~ no ]] && echo linux
(same as in bash
).
setopt extendedglob
[[ $LINUX_CONF = (#i)no ]] && echo linux
(less dangerous than making all matches case insensitive)
[[ $(L)LINUX_CONF = no ]] && echo linux
(convert to lowercase operator)
set -o rematchpcre
[[ $LINUX_CONF =~ (?i)no ]]
(PCRE syntax)
ksh93
[[ $LINUX_CONF = ~(i)no ]]
or
[[ $LINUX_CONF = ~(i:no) ]]
Note that all approaches above other than [nN][oO]
to do case insensitive matching depend on the user's locale. Not all people around the world agree on what the uppercase version of a given letter is, even for ASCII ones.
In practice for the ASCII ones, at least on GNU systems, the deviations from the English rules seem to be limited to the i
and I
letters and whether the dot is there or not on the uppercase or lowercase version.
What that means is that [[ $VAR,, = oui ]]
is not guaranteed to match on OUI
in every locale (even when the bug in current versions of bash
is fixed).
No need to use that ksh
-style [[...]]
command, you can use the standard sh
case
construct here:
case $LINUX_CONF in
([Nn][Oo]) echo linux;;
(*) echo not linux;;
esac
Or naming each possible case individually:
case $LINUX_CONF in
(No | nO | NO | no) echo linux;;
(*) echo not linux;;
esac
For a bash
-specific way to do case-insensitive matching, you can do:
shopt -s nocasematch
[[ $LINUX_CONF = no ]] && echo linux
Or:
[[ $LINUX_CONF,, = no ]] && echo linux
(where $VAR,,
is the syntax to convert a string to lower case).
You can also force a variable to be converted to lowercase upon assignment with:
typeset -l LINUX_CONF
That also comes from ksh and is also supported by bash
and zsh
.
More variants with other shells:
zsh
set -o nocasematch
[[ $LINUX_CONF =~ no ]] && echo linux
(same as in bash
).
setopt extendedglob
[[ $LINUX_CONF = (#i)no ]] && echo linux
(less dangerous than making all matches case insensitive)
[[ $(L)LINUX_CONF = no ]] && echo linux
(convert to lowercase operator)
set -o rematchpcre
[[ $LINUX_CONF =~ (?i)no ]]
(PCRE syntax)
ksh93
[[ $LINUX_CONF = ~(i)no ]]
or
[[ $LINUX_CONF = ~(i:no) ]]
Note that all approaches above other than [nN][oO]
to do case insensitive matching depend on the user's locale. Not all people around the world agree on what the uppercase version of a given letter is, even for ASCII ones.
In practice for the ASCII ones, at least on GNU systems, the deviations from the English rules seem to be limited to the i
and I
letters and whether the dot is there or not on the uppercase or lowercase version.
What that means is that [[ $VAR,, = oui ]]
is not guaranteed to match on OUI
in every locale (even when the bug in current versions of bash
is fixed).
edited Oct 2 '17 at 19:32
answered Oct 2 '17 at 16:12
Stéphane Chazelas
283k53522859
283k53522859
ok fine but how we do the same in if syntax?
â yael
Oct 2 '17 at 16:13
@yael, why would you want to useif
? If that's for theelse
part, see edit.
â Stéphane Chazelas
Oct 2 '17 at 16:15
There is also[[ "$LINUX_CONF" =~ [Nn][Oo] ]]
(what with the OP specifically asking for a RE).
â DopeGhoti
Oct 2 '17 at 16:21
1
@DopeGhoti, or[[ $LINUX_CONF = [Nn][Oo] ]]
but that has little advantage over the standardcase
syntax.
â Stéphane Chazelas
Oct 2 '17 at 16:22
In this case, I agree for such a simple comparison. I, as you did, would simply squash to lower case wtih$var,,
.
â DopeGhoti
Oct 2 '17 at 16:23
add a comment |Â
ok fine but how we do the same in if syntax?
â yael
Oct 2 '17 at 16:13
@yael, why would you want to useif
? If that's for theelse
part, see edit.
â Stéphane Chazelas
Oct 2 '17 at 16:15
There is also[[ "$LINUX_CONF" =~ [Nn][Oo] ]]
(what with the OP specifically asking for a RE).
â DopeGhoti
Oct 2 '17 at 16:21
1
@DopeGhoti, or[[ $LINUX_CONF = [Nn][Oo] ]]
but that has little advantage over the standardcase
syntax.
â Stéphane Chazelas
Oct 2 '17 at 16:22
In this case, I agree for such a simple comparison. I, as you did, would simply squash to lower case wtih$var,,
.
â DopeGhoti
Oct 2 '17 at 16:23
ok fine but how we do the same in if syntax?
â yael
Oct 2 '17 at 16:13
ok fine but how we do the same in if syntax?
â yael
Oct 2 '17 at 16:13
@yael, why would you want to use
if
? If that's for the else
part, see edit.â Stéphane Chazelas
Oct 2 '17 at 16:15
@yael, why would you want to use
if
? If that's for the else
part, see edit.â Stéphane Chazelas
Oct 2 '17 at 16:15
There is also
[[ "$LINUX_CONF" =~ [Nn][Oo] ]]
(what with the OP specifically asking for a RE).â DopeGhoti
Oct 2 '17 at 16:21
There is also
[[ "$LINUX_CONF" =~ [Nn][Oo] ]]
(what with the OP specifically asking for a RE).â DopeGhoti
Oct 2 '17 at 16:21
1
1
@DopeGhoti, or
[[ $LINUX_CONF = [Nn][Oo] ]]
but that has little advantage over the standard case
syntax.â Stéphane Chazelas
Oct 2 '17 at 16:22
@DopeGhoti, or
[[ $LINUX_CONF = [Nn][Oo] ]]
but that has little advantage over the standard case
syntax.â Stéphane Chazelas
Oct 2 '17 at 16:22
In this case, I agree for such a simple comparison. I, as you did, would simply squash to lower case wtih
$var,,
.â DopeGhoti
Oct 2 '17 at 16:23
In this case, I agree for such a simple comparison. I, as you did, would simply squash to lower case wtih
$var,,
.â DopeGhoti
Oct 2 '17 at 16:23
add a comment |Â
up vote
0
down vote
Keep your existing command but on the line before it run this:
LINUX_CONF=$(echo $LINUX_CONF | awk ' print tolower($0) ')
Regardless of the case of the value stored in your variable this will force the replacement value to be lowercase. This results in matching your existing command with only one additional line of code.
add a comment |Â
up vote
0
down vote
Keep your existing command but on the line before it run this:
LINUX_CONF=$(echo $LINUX_CONF | awk ' print tolower($0) ')
Regardless of the case of the value stored in your variable this will force the replacement value to be lowercase. This results in matching your existing command with only one additional line of code.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Keep your existing command but on the line before it run this:
LINUX_CONF=$(echo $LINUX_CONF | awk ' print tolower($0) ')
Regardless of the case of the value stored in your variable this will force the replacement value to be lowercase. This results in matching your existing command with only one additional line of code.
Keep your existing command but on the line before it run this:
LINUX_CONF=$(echo $LINUX_CONF | awk ' print tolower($0) ')
Regardless of the case of the value stored in your variable this will force the replacement value to be lowercase. This results in matching your existing command with only one additional line of code.
answered Oct 3 '17 at 11:56
EnterUserNameHere
4318
4318
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f395685%2fbash-case-insensitive-matching-of-variable%23new-answer', 'question_page');
);
Post as a guest
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
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
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