4 ms·
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 exp
by floatingatoll 9y ago
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
$
- Hello71 9y agothis 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.
- floatingatoll 9y agoI 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.