Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The advice to double-quote everything is an interesting way to circumvent "detailed knowledge may be required". I wish it noted that you can't quote regular expressions, though:

  $ cat - > foo
  #!/bin/bash
  a="BCD"
  [[ $a =~  .C.  ]] && echo 1
  [[ $a =~ ".C." ]] && echo 2
  ^D
  $ bash foo
  1
  $


this is intentional, since you can store your regular expression in a variable:

    $ a=BCD; b=.C.; [[ $a =~ $b ]]; echo $?; [[ $a =~ "$b" ]]; echo $?
otherwise, interpolating variables in regular expressions as text would require other syntax (more confusing).

also, "cat -" is redundant, use "cat". this behavior is specified by POSIX.


I use “cat -“ so that my code makes more sense to people. I want STDIN declared somehow, and the dash is effective. Technically I shouldn’t have bothered with the cat at all in an HN code snippet, but it was a courtesy to provide a familiar environment for the block I wanted to convey. It worked so well that you linted it! I really appreciate the thought.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: