2

I frequently make use of shell file name globbing to do things like operate on all of /dev/sda, /dev/sdb, /dev/sdc, etc by doing /dev/sd[a-f], however, this relies on there being files on disk that match that pattern. Today I wanted to run gitk on each of master@{1}, master@{2}, etc, but gitk master@{[1-5]} does not work since there are no file names matching that pattern on disk.

How can you get the shell to perform globbing without regard to actual disk files?

  • 1
    Which shell do you use? – Mischa Arefiev Mar 24 '15 at 6:39
  • I'm not sure what you're asking. Do you want a command like fsck /dev/sb[a-f] (i.e., misspelled sd) to not fail even though there are no sb devices? Or are you saying that you want to be able to generate command arguments like psusi1, psusi2, psusi3, etc., even though there are no such files? – Scott Mar 24 '15 at 7:37
3

You can't do shell globbing without matching files. You need brace expansion:

gitk master@{{1..5}}

or:

gitk master@{{1,2,3,4,5}}

With form {x..y}, you need zsh 2.6-beta4 and later, bash 3.0 and later, ksh93r and later.

{x,y} was original in csh and copied to Bourne like shell first in ksh.

1

In at least bash you can use ranges, e.g.

$ echo gitk master@{{1..5}}
gitk master@{1} master@{2} master@{3} master@{4} master@{5}

Obviously the echo is used above just to show what happens.

It works for letters as well:

$ echo /dev/sd{x..z}
/dev/sdx /dev/sdy /dev/sdz

Note: the is glob matching, and there is regex matching. These are two distinct things. The shell does glob matching. So Shell regex globbing is not a thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.