Using case and arrays together in bash

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












Is it possible to check if a variable is contained inside an array using case? I would like to do something like



ARR=( opt1 opt2 opt3 );

case $1 in
$ARR)
echo "Option is contained in the array";
*)
echo "Option is not contained in the array";
esac






share|improve this question


















  • 1




    Are you mandated to use case?
    – Inian
    Dec 15 '17 at 8:57










  • Possible duplicate of How do I test if an item is in a bash array?
    – muru
    Dec 15 '17 at 12:21










  • @muru, I'd say it's different here because of the case requirement. Presumably in the end the OP wants to add more cases as in case $1 in $ARR1)... $ARR2)... foo) bar)...; esac
    – Stéphane Chazelas
    Dec 15 '17 at 13:10














up vote
2
down vote

favorite












Is it possible to check if a variable is contained inside an array using case? I would like to do something like



ARR=( opt1 opt2 opt3 );

case $1 in
$ARR)
echo "Option is contained in the array";
*)
echo "Option is not contained in the array";
esac






share|improve this question


















  • 1




    Are you mandated to use case?
    – Inian
    Dec 15 '17 at 8:57










  • Possible duplicate of How do I test if an item is in a bash array?
    – muru
    Dec 15 '17 at 12:21










  • @muru, I'd say it's different here because of the case requirement. Presumably in the end the OP wants to add more cases as in case $1 in $ARR1)... $ARR2)... foo) bar)...; esac
    – Stéphane Chazelas
    Dec 15 '17 at 13:10












up vote
2
down vote

favorite









up vote
2
down vote

favorite











Is it possible to check if a variable is contained inside an array using case? I would like to do something like



ARR=( opt1 opt2 opt3 );

case $1 in
$ARR)
echo "Option is contained in the array";
*)
echo "Option is not contained in the array";
esac






share|improve this question














Is it possible to check if a variable is contained inside an array using case? I would like to do something like



ARR=( opt1 opt2 opt3 );

case $1 in
$ARR)
echo "Option is contained in the array";
*)
echo "Option is not contained in the array";
esac








share|improve this question













share|improve this question




share|improve this question








edited Dec 19 '17 at 1:31









Jeff Schaller

31.9k848109




31.9k848109










asked Dec 15 '17 at 8:46









red_trumpet

1567




1567







  • 1




    Are you mandated to use case?
    – Inian
    Dec 15 '17 at 8:57










  • Possible duplicate of How do I test if an item is in a bash array?
    – muru
    Dec 15 '17 at 12:21










  • @muru, I'd say it's different here because of the case requirement. Presumably in the end the OP wants to add more cases as in case $1 in $ARR1)... $ARR2)... foo) bar)...; esac
    – Stéphane Chazelas
    Dec 15 '17 at 13:10












  • 1




    Are you mandated to use case?
    – Inian
    Dec 15 '17 at 8:57










  • Possible duplicate of How do I test if an item is in a bash array?
    – muru
    Dec 15 '17 at 12:21










  • @muru, I'd say it's different here because of the case requirement. Presumably in the end the OP wants to add more cases as in case $1 in $ARR1)... $ARR2)... foo) bar)...; esac
    – Stéphane Chazelas
    Dec 15 '17 at 13:10







1




1




Are you mandated to use case?
– Inian
Dec 15 '17 at 8:57




Are you mandated to use case?
– Inian
Dec 15 '17 at 8:57












Possible duplicate of How do I test if an item is in a bash array?
– muru
Dec 15 '17 at 12:21




Possible duplicate of How do I test if an item is in a bash array?
– muru
Dec 15 '17 at 12:21












@muru, I'd say it's different here because of the case requirement. Presumably in the end the OP wants to add more cases as in case $1 in $ARR1)... $ARR2)... foo) bar)...; esac
– Stéphane Chazelas
Dec 15 '17 at 13:10




@muru, I'd say it's different here because of the case requirement. Presumably in the end the OP wants to add more cases as in case $1 in $ARR1)... $ARR2)... foo) bar)...; esac
– Stéphane Chazelas
Dec 15 '17 at 13:10










3 Answers
3






active

oldest

votes

















up vote
4
down vote













With ksh93, thanks to this bug, you can do:



IFS='|'
ARR=( opt1 opt2 opt3 )

IFS='|'
case $1 in
(@("$ARR[*]"))
echo "Option is contained in the array";;
(*)
echo "Option is not contained in the array";;
esac


(I wouldn't rely on it as the bug might get fixed in the future).



With zsh, you could do:



case $ARR[(Ie)$1]
(0)
echo "Option is not contained in the array";;
(*)
echo "Option is contained in the array";;
esac


(though, you'd probably rather want to use if ((ARR[(Ie)$1])); then echo is present... here rather than a case construct).



$array[(I)pattern] returns the index of the last element that matches the pattern in the array, or 0 otherwise. The e flag is for exact match (as opposed to pattern match).



With bash, ksh, yash, zsh, if you're ready to assume that $ARR and $1 don't contain a certain character like @, and that $ARR won't be empty, you can do:



IFS=@
case "@$ARR[*]@" in
(*"@$1@"*)
echo "Option is contained in the array";;
(*)
echo "Option is not contained in the array";;
esac


With bash -O extglob, zsh -o kshglob -o globsubst, you could define a helper that builds a pattern based on the elements of the array:



arraypat() 
awk '
BEGIN
if (ARGC <= 1) print "!(*)"
else
for (i = 1; i < ARGC; i++)
gsub(/[
print "@(" s ")"

' "$@"


case $1 in
($(arraypat "$ARR[@]"))
echo "Option is contained in the array";;
(*)
echo "Option is not contained in the array";;
esac





share|improve this answer






















  • @glennjackman, oops. Thanks. Well spotted.
    – Stéphane Chazelas
    Dec 15 '17 at 11:03

















up vote
3
down vote













Not really in a compact and easy to use way. Remember that $ARR will expand to only the first element of the array, opt1 in your example.



You could use "$ARR[@]", but using your data this would give a false positive for the string 1 opt.



With more recent versions of bash, you could consider using an associative array:



declare -A arr
arr=( [opt1]=1 [opt2]=1 [opt3]=1 )

if [[ "$arr[$1]" -eq 1 ]]; then
# $1 is a key in arr
else
# is not
fi





share|improve this answer






















  • Or to get full use of the associative array, i.e. use arbitrary values, not just 1, test with something like [[ "$arr[$1]+x" ]] (it's shorter, too!)
    – ilkkachu
    Dec 15 '17 at 10:44










  • @ilkkachu Yes, or [[ -n "$arr[$1]" ]].
    – Kusalananda
    Dec 15 '17 at 10:49

















up vote
2
down vote













Why would you want to do it with case? It's meant for string pattern matching, not per-element matching.



Frankly, if you need the "contains" test often and want to make it short because of that, just put the hard part in a function instead of using ugly workarounds:



#!/bin/bash
ARR=( foo bar doo );

contains()
typeset _x;
typeset -n _A="$1"
for _x in "$_A[@]" ; do
[ "$_x" = "$2" ] && return 0
done
return 1


if contains ARR "$1" ; then
echo ""$1" is contained in ARR"
else
echo ""$1" not contained in ARR"
fi


(That should also work in ksh)






share|improve this answer






















  • OP probably wants a builtin for the presumed speed and efficiency.
    – RonJohn
    Dec 15 '17 at 13:26










  • @RonJohn, everything that function does, is built in to Bash. As for speed, it's hard to say, we'd need to test, but for huge data sets, associative arrays would probably be faster (than either the loop, or a case), since key lookups are presumably O(1). But for huge data, you shouldn't use the shell anyway, and for small data, minor performance differences don't matter.
    – ilkkachu
    Dec 15 '17 at 14:04











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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f411001%2fusing-case-and-arrays-together-in-bash%23new-answer', 'question_page');

);

Post as a guest






























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote













With ksh93, thanks to this bug, you can do:



IFS='|'
ARR=( opt1 opt2 opt3 )

IFS='|'
case $1 in
(@("$ARR[*]"))
echo "Option is contained in the array";;
(*)
echo "Option is not contained in the array";;
esac


(I wouldn't rely on it as the bug might get fixed in the future).



With zsh, you could do:



case $ARR[(Ie)$1]
(0)
echo "Option is not contained in the array";;
(*)
echo "Option is contained in the array";;
esac


(though, you'd probably rather want to use if ((ARR[(Ie)$1])); then echo is present... here rather than a case construct).



$array[(I)pattern] returns the index of the last element that matches the pattern in the array, or 0 otherwise. The e flag is for exact match (as opposed to pattern match).



With bash, ksh, yash, zsh, if you're ready to assume that $ARR and $1 don't contain a certain character like @, and that $ARR won't be empty, you can do:



IFS=@
case "@$ARR[*]@" in
(*"@$1@"*)
echo "Option is contained in the array";;
(*)
echo "Option is not contained in the array";;
esac


With bash -O extglob, zsh -o kshglob -o globsubst, you could define a helper that builds a pattern based on the elements of the array:



arraypat() 
awk '
BEGIN
if (ARGC <= 1) print "!(*)"
else
for (i = 1; i < ARGC; i++)
gsub(/[
print "@(" s ")"

' "$@"


case $1 in
($(arraypat "$ARR[@]"))
echo "Option is contained in the array";;
(*)
echo "Option is not contained in the array";;
esac





share|improve this answer






















  • @glennjackman, oops. Thanks. Well spotted.
    – Stéphane Chazelas
    Dec 15 '17 at 11:03














up vote
4
down vote













With ksh93, thanks to this bug, you can do:



IFS='|'
ARR=( opt1 opt2 opt3 )

IFS='|'
case $1 in
(@("$ARR[*]"))
echo "Option is contained in the array";;
(*)
echo "Option is not contained in the array";;
esac


(I wouldn't rely on it as the bug might get fixed in the future).



With zsh, you could do:



case $ARR[(Ie)$1]
(0)
echo "Option is not contained in the array";;
(*)
echo "Option is contained in the array";;
esac


(though, you'd probably rather want to use if ((ARR[(Ie)$1])); then echo is present... here rather than a case construct).



$array[(I)pattern] returns the index of the last element that matches the pattern in the array, or 0 otherwise. The e flag is for exact match (as opposed to pattern match).



With bash, ksh, yash, zsh, if you're ready to assume that $ARR and $1 don't contain a certain character like @, and that $ARR won't be empty, you can do:



IFS=@
case "@$ARR[*]@" in
(*"@$1@"*)
echo "Option is contained in the array";;
(*)
echo "Option is not contained in the array";;
esac


With bash -O extglob, zsh -o kshglob -o globsubst, you could define a helper that builds a pattern based on the elements of the array:



arraypat() 
awk '
BEGIN
if (ARGC <= 1) print "!(*)"
else
for (i = 1; i < ARGC; i++)
gsub(/[
print "@(" s ")"

' "$@"


case $1 in
($(arraypat "$ARR[@]"))
echo "Option is contained in the array";;
(*)
echo "Option is not contained in the array";;
esac





share|improve this answer






















  • @glennjackman, oops. Thanks. Well spotted.
    – Stéphane Chazelas
    Dec 15 '17 at 11:03












up vote
4
down vote










up vote
4
down vote









With ksh93, thanks to this bug, you can do:



IFS='|'
ARR=( opt1 opt2 opt3 )

IFS='|'
case $1 in
(@("$ARR[*]"))
echo "Option is contained in the array";;
(*)
echo "Option is not contained in the array";;
esac


(I wouldn't rely on it as the bug might get fixed in the future).



With zsh, you could do:



case $ARR[(Ie)$1]
(0)
echo "Option is not contained in the array";;
(*)
echo "Option is contained in the array";;
esac


(though, you'd probably rather want to use if ((ARR[(Ie)$1])); then echo is present... here rather than a case construct).



$array[(I)pattern] returns the index of the last element that matches the pattern in the array, or 0 otherwise. The e flag is for exact match (as opposed to pattern match).



With bash, ksh, yash, zsh, if you're ready to assume that $ARR and $1 don't contain a certain character like @, and that $ARR won't be empty, you can do:



IFS=@
case "@$ARR[*]@" in
(*"@$1@"*)
echo "Option is contained in the array";;
(*)
echo "Option is not contained in the array";;
esac


With bash -O extglob, zsh -o kshglob -o globsubst, you could define a helper that builds a pattern based on the elements of the array:



arraypat() 
awk '
BEGIN
if (ARGC <= 1) print "!(*)"
else
for (i = 1; i < ARGC; i++)
gsub(/[
print "@(" s ")"

' "$@"


case $1 in
($(arraypat "$ARR[@]"))
echo "Option is contained in the array";;
(*)
echo "Option is not contained in the array";;
esac





share|improve this answer














With ksh93, thanks to this bug, you can do:



IFS='|'
ARR=( opt1 opt2 opt3 )

IFS='|'
case $1 in
(@("$ARR[*]"))
echo "Option is contained in the array";;
(*)
echo "Option is not contained in the array";;
esac


(I wouldn't rely on it as the bug might get fixed in the future).



With zsh, you could do:



case $ARR[(Ie)$1]
(0)
echo "Option is not contained in the array";;
(*)
echo "Option is contained in the array";;
esac


(though, you'd probably rather want to use if ((ARR[(Ie)$1])); then echo is present... here rather than a case construct).



$array[(I)pattern] returns the index of the last element that matches the pattern in the array, or 0 otherwise. The e flag is for exact match (as opposed to pattern match).



With bash, ksh, yash, zsh, if you're ready to assume that $ARR and $1 don't contain a certain character like @, and that $ARR won't be empty, you can do:



IFS=@
case "@$ARR[*]@" in
(*"@$1@"*)
echo "Option is contained in the array";;
(*)
echo "Option is not contained in the array";;
esac


With bash -O extglob, zsh -o kshglob -o globsubst, you could define a helper that builds a pattern based on the elements of the array:



arraypat() 
awk '
BEGIN
if (ARGC <= 1) print "!(*)"
else
for (i = 1; i < ARGC; i++)
gsub(/[
print "@(" s ")"

' "$@"


case $1 in
($(arraypat "$ARR[@]"))
echo "Option is contained in the array";;
(*)
echo "Option is not contained in the array";;
esac






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 15 '17 at 13:20

























answered Dec 15 '17 at 9:30









Stéphane Chazelas

282k53520854




282k53520854











  • @glennjackman, oops. Thanks. Well spotted.
    – Stéphane Chazelas
    Dec 15 '17 at 11:03
















  • @glennjackman, oops. Thanks. Well spotted.
    – Stéphane Chazelas
    Dec 15 '17 at 11:03















@glennjackman, oops. Thanks. Well spotted.
– Stéphane Chazelas
Dec 15 '17 at 11:03




@glennjackman, oops. Thanks. Well spotted.
– Stéphane Chazelas
Dec 15 '17 at 11:03












up vote
3
down vote













Not really in a compact and easy to use way. Remember that $ARR will expand to only the first element of the array, opt1 in your example.



You could use "$ARR[@]", but using your data this would give a false positive for the string 1 opt.



With more recent versions of bash, you could consider using an associative array:



declare -A arr
arr=( [opt1]=1 [opt2]=1 [opt3]=1 )

if [[ "$arr[$1]" -eq 1 ]]; then
# $1 is a key in arr
else
# is not
fi





share|improve this answer






















  • Or to get full use of the associative array, i.e. use arbitrary values, not just 1, test with something like [[ "$arr[$1]+x" ]] (it's shorter, too!)
    – ilkkachu
    Dec 15 '17 at 10:44










  • @ilkkachu Yes, or [[ -n "$arr[$1]" ]].
    – Kusalananda
    Dec 15 '17 at 10:49














up vote
3
down vote













Not really in a compact and easy to use way. Remember that $ARR will expand to only the first element of the array, opt1 in your example.



You could use "$ARR[@]", but using your data this would give a false positive for the string 1 opt.



With more recent versions of bash, you could consider using an associative array:



declare -A arr
arr=( [opt1]=1 [opt2]=1 [opt3]=1 )

if [[ "$arr[$1]" -eq 1 ]]; then
# $1 is a key in arr
else
# is not
fi





share|improve this answer






















  • Or to get full use of the associative array, i.e. use arbitrary values, not just 1, test with something like [[ "$arr[$1]+x" ]] (it's shorter, too!)
    – ilkkachu
    Dec 15 '17 at 10:44










  • @ilkkachu Yes, or [[ -n "$arr[$1]" ]].
    – Kusalananda
    Dec 15 '17 at 10:49












up vote
3
down vote










up vote
3
down vote









Not really in a compact and easy to use way. Remember that $ARR will expand to only the first element of the array, opt1 in your example.



You could use "$ARR[@]", but using your data this would give a false positive for the string 1 opt.



With more recent versions of bash, you could consider using an associative array:



declare -A arr
arr=( [opt1]=1 [opt2]=1 [opt3]=1 )

if [[ "$arr[$1]" -eq 1 ]]; then
# $1 is a key in arr
else
# is not
fi





share|improve this answer














Not really in a compact and easy to use way. Remember that $ARR will expand to only the first element of the array, opt1 in your example.



You could use "$ARR[@]", but using your data this would give a false positive for the string 1 opt.



With more recent versions of bash, you could consider using an associative array:



declare -A arr
arr=( [opt1]=1 [opt2]=1 [opt3]=1 )

if [[ "$arr[$1]" -eq 1 ]]; then
# $1 is a key in arr
else
# is not
fi






share|improve this answer














share|improve this answer



share|improve this answer








edited May 5 at 21:22

























answered Dec 15 '17 at 8:59









Kusalananda

104k14206324




104k14206324











  • Or to get full use of the associative array, i.e. use arbitrary values, not just 1, test with something like [[ "$arr[$1]+x" ]] (it's shorter, too!)
    – ilkkachu
    Dec 15 '17 at 10:44










  • @ilkkachu Yes, or [[ -n "$arr[$1]" ]].
    – Kusalananda
    Dec 15 '17 at 10:49
















  • Or to get full use of the associative array, i.e. use arbitrary values, not just 1, test with something like [[ "$arr[$1]+x" ]] (it's shorter, too!)
    – ilkkachu
    Dec 15 '17 at 10:44










  • @ilkkachu Yes, or [[ -n "$arr[$1]" ]].
    – Kusalananda
    Dec 15 '17 at 10:49















Or to get full use of the associative array, i.e. use arbitrary values, not just 1, test with something like [[ "$arr[$1]+x" ]] (it's shorter, too!)
– ilkkachu
Dec 15 '17 at 10:44




Or to get full use of the associative array, i.e. use arbitrary values, not just 1, test with something like [[ "$arr[$1]+x" ]] (it's shorter, too!)
– ilkkachu
Dec 15 '17 at 10:44












@ilkkachu Yes, or [[ -n "$arr[$1]" ]].
– Kusalananda
Dec 15 '17 at 10:49




@ilkkachu Yes, or [[ -n "$arr[$1]" ]].
– Kusalananda
Dec 15 '17 at 10:49










up vote
2
down vote













Why would you want to do it with case? It's meant for string pattern matching, not per-element matching.



Frankly, if you need the "contains" test often and want to make it short because of that, just put the hard part in a function instead of using ugly workarounds:



#!/bin/bash
ARR=( foo bar doo );

contains()
typeset _x;
typeset -n _A="$1"
for _x in "$_A[@]" ; do
[ "$_x" = "$2" ] && return 0
done
return 1


if contains ARR "$1" ; then
echo ""$1" is contained in ARR"
else
echo ""$1" not contained in ARR"
fi


(That should also work in ksh)






share|improve this answer






















  • OP probably wants a builtin for the presumed speed and efficiency.
    – RonJohn
    Dec 15 '17 at 13:26










  • @RonJohn, everything that function does, is built in to Bash. As for speed, it's hard to say, we'd need to test, but for huge data sets, associative arrays would probably be faster (than either the loop, or a case), since key lookups are presumably O(1). But for huge data, you shouldn't use the shell anyway, and for small data, minor performance differences don't matter.
    – ilkkachu
    Dec 15 '17 at 14:04















up vote
2
down vote













Why would you want to do it with case? It's meant for string pattern matching, not per-element matching.



Frankly, if you need the "contains" test often and want to make it short because of that, just put the hard part in a function instead of using ugly workarounds:



#!/bin/bash
ARR=( foo bar doo );

contains()
typeset _x;
typeset -n _A="$1"
for _x in "$_A[@]" ; do
[ "$_x" = "$2" ] && return 0
done
return 1


if contains ARR "$1" ; then
echo ""$1" is contained in ARR"
else
echo ""$1" not contained in ARR"
fi


(That should also work in ksh)






share|improve this answer






















  • OP probably wants a builtin for the presumed speed and efficiency.
    – RonJohn
    Dec 15 '17 at 13:26










  • @RonJohn, everything that function does, is built in to Bash. As for speed, it's hard to say, we'd need to test, but for huge data sets, associative arrays would probably be faster (than either the loop, or a case), since key lookups are presumably O(1). But for huge data, you shouldn't use the shell anyway, and for small data, minor performance differences don't matter.
    – ilkkachu
    Dec 15 '17 at 14:04













up vote
2
down vote










up vote
2
down vote









Why would you want to do it with case? It's meant for string pattern matching, not per-element matching.



Frankly, if you need the "contains" test often and want to make it short because of that, just put the hard part in a function instead of using ugly workarounds:



#!/bin/bash
ARR=( foo bar doo );

contains()
typeset _x;
typeset -n _A="$1"
for _x in "$_A[@]" ; do
[ "$_x" = "$2" ] && return 0
done
return 1


if contains ARR "$1" ; then
echo ""$1" is contained in ARR"
else
echo ""$1" not contained in ARR"
fi


(That should also work in ksh)






share|improve this answer














Why would you want to do it with case? It's meant for string pattern matching, not per-element matching.



Frankly, if you need the "contains" test often and want to make it short because of that, just put the hard part in a function instead of using ugly workarounds:



#!/bin/bash
ARR=( foo bar doo );

contains()
typeset _x;
typeset -n _A="$1"
for _x in "$_A[@]" ; do
[ "$_x" = "$2" ] && return 0
done
return 1


if contains ARR "$1" ; then
echo ""$1" is contained in ARR"
else
echo ""$1" not contained in ARR"
fi


(That should also work in ksh)







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 15 '17 at 11:16

























answered Dec 15 '17 at 10:49









ilkkachu

49.9k674137




49.9k674137











  • OP probably wants a builtin for the presumed speed and efficiency.
    – RonJohn
    Dec 15 '17 at 13:26










  • @RonJohn, everything that function does, is built in to Bash. As for speed, it's hard to say, we'd need to test, but for huge data sets, associative arrays would probably be faster (than either the loop, or a case), since key lookups are presumably O(1). But for huge data, you shouldn't use the shell anyway, and for small data, minor performance differences don't matter.
    – ilkkachu
    Dec 15 '17 at 14:04

















  • OP probably wants a builtin for the presumed speed and efficiency.
    – RonJohn
    Dec 15 '17 at 13:26










  • @RonJohn, everything that function does, is built in to Bash. As for speed, it's hard to say, we'd need to test, but for huge data sets, associative arrays would probably be faster (than either the loop, or a case), since key lookups are presumably O(1). But for huge data, you shouldn't use the shell anyway, and for small data, minor performance differences don't matter.
    – ilkkachu
    Dec 15 '17 at 14:04
















OP probably wants a builtin for the presumed speed and efficiency.
– RonJohn
Dec 15 '17 at 13:26




OP probably wants a builtin for the presumed speed and efficiency.
– RonJohn
Dec 15 '17 at 13:26












@RonJohn, everything that function does, is built in to Bash. As for speed, it's hard to say, we'd need to test, but for huge data sets, associative arrays would probably be faster (than either the loop, or a case), since key lookups are presumably O(1). But for huge data, you shouldn't use the shell anyway, and for small data, minor performance differences don't matter.
– ilkkachu
Dec 15 '17 at 14:04





@RonJohn, everything that function does, is built in to Bash. As for speed, it's hard to say, we'd need to test, but for huge data sets, associative arrays would probably be faster (than either the loop, or a case), since key lookups are presumably O(1). But for huge data, you shouldn't use the shell anyway, and for small data, minor performance differences don't matter.
– ilkkachu
Dec 15 '17 at 14:04













 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f411001%2fusing-case-and-arrays-together-in-bash%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay