My directory variable

POSTMAP="/work/Documents/Projects/untitled\ folder/untitled\ folder/*/*_tsta.bam"

My for statement:

for file0 in ${POSTMAP}; do

It seems that the whitespace in 'untitled folder' messes with the globbing. I suspect this because file0 ends up being '/untitled'. Note that I have 'shopt -s extglob'.


It's not really messing up with the globbing. Here, by using $POSTMAP unquoted, you're using the split+glob operator.

With the default value of $IFS, on your /work/Documents/Projects/untitled\ folder/untitled\ folder/*/*_tsta.bam, it will first split it into "/work/Documents/Projects/untitled\", "folder/untitled\" and "folder/*/*_tsta.bam". Only the third one contains wildcard characters and thus be subject to the glob part. However, the glob would just search for files in the folder directory relative to the current directory.

If you only want the glob part and not the split of that split+glob operator, set $IFS to the empty string. For that operator, backslash can't be used to escape $IFS separators (with bash (and bash only among Bourne-like shells), it can be used to escape wildcard glob operators though).

So either:

POSTMAP="/work/Documents/Projects/untitled folder/untitled folder/*/*_tsta.bam"
IFS=   # don't split
set +f # do glob
for file0 in $POSTMAP # invoke the split+glob operator

Or probably better here with shells supporting arrays like bash, yash, zsh, ksh:

  '/work/Documents/Projects/untitled folder/untitled folder/'*/*_tsta.bam
) # expand the glob at the time of that array assignment
for file0 in "${postmap[@]}" # loop over the array elements
  • Thank you. For the second suggestion can you place the closing apostrophe after .bam like ('/work/Documents/Projects/untitled folder/untitled folder/*/*_tsta.bam')? Also what is the advantage of using the 2nd rather than 1st suggestion for the purpose of building a for loop? – ozarka Feb 19 '16 at 9:50
  • 1
    @mattqdean. No. Obviously, globs are not expanded within quotes. You can quote whatever you like except those wildcard characters with whatever type of quoting you like. Like postmap=('/work/Do'"cume"\n\t\s\/$'Project'$"s/untitled fo"'lder/untitled folder/'*"/"*'_tsta.bam'), here alternating (for no good reason except demonstration) the different types of shell quotes ('...', backslash, "...", $'...', $"..."). – Stéphane Chazelas Feb 19 '16 at 10:58

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.