Forcing whitespace in Bash arrays being passed into grep -
i have pretty simple bash script should grep file multiple phrases want flag.
it works until point, falling on when want grep ' print ' or ' puts ' (note whitespace before , after word).
the grep ignoring whitespace in input.
here code (stuff isn't relevant has been cut out)
#!/bin/sh bad_phrases=('console.log(' 'binding.pry' ':focus,' 'alert(' ' print ' ' puts ') bad_phrase_found=false file='my_test_file.txt' bad_phrase in ${bad_phrases[*]} ; if grep -q "$bad_phrase" $file ; bad_phrase_found=true echo "a '$(tput bold)$(tput setaf 1)$bad_phrase$(tput sgr0)' found in $(tput bold)$(tput setaf 5)$file$(tput sgr0)" fi done if $bad_phrase_found ; exit 1 fi exit 0
i have looked @ setting ifs '~' , splitting array way, killed grep command completely.
the example output of script is;
'print' found in my_test_file.txt
any appreciated.
you need use "${bad_phrases[@]}"
. don't forget double quotes. should make habit quote variables in order suppress word splitting.
for bad_phrase in "${bad_phrases[@]}" ; if grep -q "$bad_phrase" "$file" ; bad_phrase_found=true echo "a '$(tput bold)$(tput setaf 1)$bad_phrase$(tput sgr0)' found in $(tput bold)$(tput setaf 5)$file$(tput sgr0)" fi done