Quantcast
Channel: User Kamil Maciorowski - Super User
Viewing all articles
Browse latest Browse all 669

Answer by Kamil Maciorowski for How to use the `tax[13]-eq-dc` regex in a Bash loop?

$
0
0

tax[13]-eq-dc may look like a regex but in your code Bash does not treat it as such. Even if it did, a regex is to match something. What do you want to match?

Bash treats your tax[13]-eq-dc as a filename expansion (globbing) pattern. It's not a regex, although [13] in it is somewhat similar to [13] in regular expressions. Like regex, filename expansion is also designed to match something; it's designed to match filenames.

It so happens in your current working directory there are no files with names matching the pattern, so the string is not expanded and ultimately ping gets tax[13]-eq-dc as an argument. If you had a file named tax1-eq-dc or a file named tax3-eq-dc, then the pattern would be expanded to one or two words, to all the matching names of existing files, and you would run one or two pings trying to ping tax1-eq-dc or tax3-eq-dc or both.

Relying on existence of files makes no sense here. Most likely you do not want to match filenames. You probably want a brace expansion: tax{1,3}-eq-dc. This expands to two separate words: tax1-eq-dc and tax3-eq-dc, and it's a purely textual expansion that has nothing to do with files.

The filename expansion is a portable feature, the brace expansion is not.

for i in tax{1,3}-eq-dc; do ping -c3 "$i"; done

Viewing all articles
Browse latest Browse all 669

Trending Articles