2

I'm trying to write a condition that would fit all the lines starting with a space/tab and the word Path

/sPath.* - simple regexp?

I found that in Bash 4.* it should look like:

if [[ $LINE =~ "[[:space:]]Path" ]]

But this condition for some reason does not work.

if [[ $LINE =~ [[:space:]] ]]

work fine, and displays all lines with spaces/tabs.

Roberto Reale
  • 4,247
  • 1
  • 17
  • 21
aranel
  • 191
  • 1
  • 1
  • 8

2 Answers2

2

From version 3.2 onwards, the pattern (i.e., the regular expression) must not be quoted in Bash:

  1. New Features in Bash

...

f. Quoting the string argument to the [[ command's =~ operator now forces string matching, as with the other pattern-matching operators.

In other words, quoting is considered part of the regular expression itself (literal ").

Moreover, it would be better to quote the variable $LINE, to prevent errors should it be empty:

if [[ "$LINE" =~ [[:space:]] ]]
Roberto Reale
  • 4,247
  • 1
  • 17
  • 21
0

It is better to use:

[[ "$LINE" =~ [[:blank:]] ]]
  1. Quote the variable LINE
  2. Match with character class [[:blank:]] which is equivalent of space OR tab
anubhava
  • 761,203
  • 64
  • 569
  • 643