Skip to contentSkip to conversation

glob pattern and redirected input file name

DISCUSSION Jack Bates 2 participants 2 messages Aug 29, 2013 – Aug 30, 2013

lore
Share & tools
lore

Jack Bates

author
#0 lore raw

What is DASH supposed to do when input is redirected from a file, and the file name is a glob pattern? e.g.

tar xz < foo-*.tar.gz

Is it supposed to expand the glob pattern, or is that not supported?

The following both work, is there a better workaround?

tar fxz foo-*.tar.gz

   tar xz < $(echo foo-*.tar.gz)
#1 lore raw
Quoted 6 lines · Jack Bates

What is DASH supposed to do when input is redirected from a file, and the file name is a glob pattern? e.g.

tar xz < foo-*.tar.gz

Is it supposed to expand the glob pattern, or is that not supported?

Per POSIX XCU 2.7 Redirection, pathname generation may (but need not) be performed on the word after a redirection operator other than << or <<- if the shell is interactive and one word would result.

Dash chooses the option that results in the smallest code: never performing pathname generation in this case.

Quoted 5 lines

The following both work, is there a better workaround?

tar fxz foo-*.tar.gz

   tar xz < $(echo foo-*.tar.gz)

These are both concise methods. The former's problem is that it does something strange if more than one file matches. The second has problems with pathnames starting with '-', containing backslashes or ending with newlines.

In a script you might do

    set -- foo-*.tar.gz
    if [ "$#" -ne 1 ] || [ ! -f "$1" ]; then
        echo "Bad wildcard"
        exit 2
    fi
    tar -xzf "$1"
Signature · 1 line
Jilles Tjoelker