Skip to main content
added 148 characters in body
Source Link
Kusalananda
  • 356.3k
  • 42
  • 738
  • 1.1k

If it's ok to overwrite the list of positional parameters:

shopt -s globstar nullglob

set -- foo/**/bar.xml foo/**/baz.xml

if [ "$#" -ne 0 ]; then
    echo matches found
fi

This sets the list of positional parameters to the names matching the globbing pattern. Since nullglob is in effect, the list will be empty if no matches are found. The value of $# is the length of the list of positional parameters.

Using your idea of running it in a subshell:

( shopt -s globstar nullglob; set -- foo/**/bar.xml foo/**/baz.xml; [ "$#" -ne 0 ] )

Using an array in place of the list of positional parameters:

shopt -s globstar nullglob

names=( foo/**/bar.xml foo/**/baz.xml )

if [ "${#names[@]}" -ne 0 ]; then
    echo matches found
fi

If it's ok to overwrite the list of positional parameters:

shopt -s globstar nullglob

set -- foo/**/bar.xml foo/**/baz.xml

if [ "$#" -ne 0 ]; then
    echo matches found
fi

This sets the list of positional parameters to the names matching the globbing pattern. Since nullglob is in effect, the list will be empty if no matches are found. The value of $# is the length of the list of positional parameters.


Using an array in place of the list of positional parameters:

shopt -s globstar nullglob

names=( foo/**/bar.xml foo/**/baz.xml )

if [ "${#names[@]}" -ne 0 ]; then
    echo matches found
fi

If it's ok to overwrite the list of positional parameters:

shopt -s globstar nullglob

set -- foo/**/bar.xml foo/**/baz.xml

if [ "$#" -ne 0 ]; then
    echo matches found
fi

This sets the list of positional parameters to the names matching the globbing pattern. Since nullglob is in effect, the list will be empty if no matches are found. The value of $# is the length of the list of positional parameters.

Using your idea of running it in a subshell:

( shopt -s globstar nullglob; set -- foo/**/bar.xml foo/**/baz.xml; [ "$#" -ne 0 ] )

Using an array in place of the list of positional parameters:

shopt -s globstar nullglob

names=( foo/**/bar.xml foo/**/baz.xml )

if [ "${#names[@]}" -ne 0 ]; then
    echo matches found
fi
Source Link
Kusalananda
  • 356.3k
  • 42
  • 738
  • 1.1k

If it's ok to overwrite the list of positional parameters:

shopt -s globstar nullglob

set -- foo/**/bar.xml foo/**/baz.xml

if [ "$#" -ne 0 ]; then
    echo matches found
fi

This sets the list of positional parameters to the names matching the globbing pattern. Since nullglob is in effect, the list will be empty if no matches are found. The value of $# is the length of the list of positional parameters.


Using an array in place of the list of positional parameters:

shopt -s globstar nullglob

names=( foo/**/bar.xml foo/**/baz.xml )

if [ "${#names[@]}" -ne 0 ]; then
    echo matches found
fi