0

I have a script which currently produces a result like this:

...
Processing !
Processing $6$vxVOJhOX$U2flG.WASP1fEsqNy1Q0S9YROgdNJi9TTC8Gn74Al4w03unxr4qtHeaeRl8sWsGLG4Om5WbUJVsqNaCD4t4tG.
Processing !
Processing $6$ehmLTmoj$VP72RBNibmjwngQxMrW0kiyax/wJHiV0ugv/8J2RJnCwNxTIBLGJ.A4t5ryZv6lQTyheoA6N0VianZC5QI7Rp1
Processing !
Processing !
...

I want to filter out entries with just !.

I'm trying the following, both with and without escaping on the \!:

while read -r line ; do
  if ! [ \$line = \! ]; then
    echo "Processing $line"
  fi
done

How do I check if a variable contains a single !?

1
  1. Your sample output has more text on the line ("Processing"), and
  2. You escaped the dollar sign in \$line, preventing variable expansion.

Try:

while read -r line; do 
  if ! [ "$line" = "Processing !" ]; then 
    echo processing line "$line"; 
  fi
done

Or, if your input is being provided by the sample script, then just compare $line to \!:

while read -r line; do 
  if ! [ "$line" = \! ]; then 
    echo processing line "$line"; 
  fi
done

or perhaps more directly:

while read -r line; do 
  if [ "$line" != \! ]; then 
    echo processing line "$line"; 
  fi
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.