2

Long story short, narrowed my problem down to a one-liner:

for a in {a..z}; do echo "-$a" | grep "\-$a"; done

This prints all letters but -e and -n.

Looks like Linux version, bash version, flags to grep (-P, -E) etc do not matter!
Tested environments:

  1. GNU bash, version 3.2.57(2)-release (x86_64-suse-linux-gnu) on SUSE Linux Enterprise Server 11 SP4
  2. GNU bash, version 4.4.12(3)-release (x86_64-unknown-cygwin) on Cygwin (base-cygwin 3.8-1; cygwin32 2.10.0-1)
  3. rextester (bash online compiler; GNU bash 4.3.46)

Why are not -e and -n printed?
(Removing the dashes in the code also removes the problem)

Dada
  • 6,313
  • 7
  • 24
  • 43
M. P.
  • 31
  • 3

1 Answers1

7

Problem is that -e, -n are valid echo options and echo is not printing them.

Moreover you should use -- in grep to separate options and pattern. Suggest you to use -F option in grep as well for fixed string search.

You may use:

for a in {a..z}; do grep -F -- "-$a" <<< "-$a"; done

-a
-b
-c
-d
-e
-f
-g
-h
-i
-j
-k
-l
-m
-n
-o
-p
-q
-r
-s
-t
-u
-v
-w
-x
-y
-z

Note that you may also use printf instead of echo:

for a in {a..z}; do printf -- '-%s\n' "$a" | grep -F -- "-$a"; done
anubhava
  • 761,203
  • 64
  • 569
  • 643