fork download
  1. #! /bin/bash
  2. set -x
  3.  
  4. elem_in_array() {
  5. local e="$1"
  6. shift
  7. local a=("$@")
  8. [[ $(printf '\x00%s\x00' "${a[@]}") =~ $(printf '\x00%s\x00' "$e") ]]
  9. }
  10.  
  11. a1=(A B "C D" E F)
  12. elem_in_array "A" "${a1[@]}" && echo Y || echo N
  13. elem_in_array "B" "${a1[@]}" && echo Y || echo N
  14. elem_in_array "C D" "${a1[@]}" && echo Y || echo N
  15.  
  16. elem_in_array "AB" "${a1[@]}" && echo BUG || echo OK
Success #stdin #stdout #stderr 0s 19664KB
stdin
Standard input is empty
stdout
Y
Y
Y
BUG
stderr
+ a1=(A B "C D" E F)
+ elem_in_array A A B 'C D' E F
+ local e=A
+ shift
+ a=("$@")
+ local a
++ printf '\x00%s\x00' A B 'C D' E F
./prog.sh: line 8: warning: command substitution: ignored null byte in input
++ printf '\x00%s\x00' A
./prog.sh: line 8: warning: command substitution: ignored null byte in input
+ [[ ABC DEF =~ A ]]
+ echo Y
+ elem_in_array B A B 'C D' E F
+ local e=B
+ shift
+ a=("$@")
+ local a
++ printf '\x00%s\x00' A B 'C D' E F
./prog.sh: line 8: warning: command substitution: ignored null byte in input
++ printf '\x00%s\x00' B
./prog.sh: line 8: warning: command substitution: ignored null byte in input
+ [[ ABC DEF =~ B ]]
+ echo Y
+ elem_in_array 'C D' A B 'C D' E F
+ local 'e=C D'
+ shift
+ a=("$@")
+ local a
++ printf '\x00%s\x00' A B 'C D' E F
./prog.sh: line 8: warning: command substitution: ignored null byte in input
++ printf '\x00%s\x00' 'C D'
./prog.sh: line 8: warning: command substitution: ignored null byte in input
+ [[ ABC DEF =~ C D ]]
+ echo Y
+ elem_in_array AB A B 'C D' E F
+ local e=AB
+ shift
+ a=("$@")
+ local a
++ printf '\x00%s\x00' A B 'C D' E F
./prog.sh: line 8: warning: command substitution: ignored null byte in input
++ printf '\x00%s\x00' AB
./prog.sh: line 8: warning: command substitution: ignored null byte in input
+ [[ ABC DEF =~ AB ]]
+ echo BUG