I use shellcheck to check over my bash shell scripts. Most of what it generates is noise, but every so often, it produces something useful - something that should be fixed/cleaned up.
I recently added some code to the script and ran shellcheck on it, and it generated the output below:
--- Cut here ---
In myScript line 145:
[[ "$REPLY" =~ ^\^ ]] && REPLY="title not like '%${REPLY:1}%'"
^-- SC1001: This \^ will be a regular '^' in this context.
^-- SC2089: Quotes/backslashes will be treated literally. Use an array.
--- Cut here ---
The intent is to check to see if $REPLY starts with a ^ and if so, change
it to the indicated string (yes, I am building up a SQL query). In my testing, I determined that the right syntax in a Bash =~ test, to check for
a leading ^, is ^\^. So, what is the SC1001 complaining about? Isn't that the right syntax to use?
Second, I don't understand the second error at all. What does "Use an
array" mean?
Note that the script works fine. There's nothing really wrong with it at all.
In myScript line 145:
[[ "$REPLY" =~ ^\^ ]] && REPLY="title not like '%${REPLY:1}%'"
^-- SC1001: This \^ will be a regular '^' in this context.
^-- SC2089: Quotes/backslashes will be treated literally. Use an array.
--- Cut here ---
The intent is to check to see if $REPLY starts with a ^ and if so, change
it to the indicated string (yes, I am building up a SQL query). In my testing, I determined that the right syntax in a Bash =~ test, to check for
a leading ^, is ^\^. So, what is the SC1001 complaining about? Isn't that
the right syntax to use?
My interpretation is that ^^ is sufficient since there's just one
single ^ to indicate the start of the line and the second ^ thus
not interpreted as another start of line but taken literally.
I read the message just as suggestion to simplify the expression.
In article <sn9ko9$g1n$1@dont-email.me>,
Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
...
My interpretation is that ^^ is sufficient since there's just one
single ^ to indicate the start of the line and the second ^ thus
not interpreted as another start of line but taken literally.
I read the message just as suggestion to simplify the expression.
In my testing, ^^ didn't work. It matched anything, not just a leading ^.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 296 |
Nodes: | 16 (2 / 14) |
Uptime: | 37:20:34 |
Calls: | 6,648 |
Calls today: | 3 |
Files: | 12,193 |
Messages: | 5,329,132 |