Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the "globs" are regexes; regular globs are supported in [] #35

Open
unhammer opened this issue Jun 10, 2016 · 0 comments
Open

the "globs" are regexes; regular globs are supported in [] #35

unhammer opened this issue Jun 10, 2016 · 0 comments

Comments

@unhammer
Copy link

unhammer commented Jun 10, 2016

The examples on globs in Tutorial.md is, as far as I can tell, wrong.

[ -f *foo* ] will do glob-matching, that is, if there is a single file in CWD with foo as a substring, this will pass. If there are more, it will (hopefully) just lead to an error "too many arguments". (Ie. don't do this.)

if [[ "${value}" =~ ".*$name.*" ]]; then
  echo 'Yo dawg, we heard you like globs'
fi

will only match if value actually has literal quotes, and will, because of the literal quotes (at least on my machine) make the .* not be interpreted as a regex.

[[ is a bash built-in, while [ is a binary in /usr/bin (to complicate things, technically, bash defines [ as a built-in with the same semantics as the command …)

[[ doesn't require quoted arguments:

$ value='a string with spaces'
$ [[ ${value} =~ .*string.* ]] && echo yes
yes

and as the regex example shows, they can (on the rhs of =~) mess things up.

Run http://www.shellcheck.net/ (has an emacs flycheck plugin if you're of that persuasion) – it catches these things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant