How do I pass a wildcard parameter to a bash file

后端 未结 2 467
时光取名叫无心
时光取名叫无心 2020-12-01 10:11

I\'m trying to write a bash script that allows the user to pass a directory path using wildcards.

For example,

bash show_files.sh *
<
2条回答
  •  有刺的猬
    2020-12-01 10:57

    The parent shell, the one invoking bash show_files.sh *, expands the * for you.

    In your script, you need to use:

    for dir in "$@"
    do
        echo "$dir"
    done
    

    The double quotes ensure that multiple spaces etc in file names are handled correctly.

    See also How to iterate over arguments in a bash shell script.


    Potentially confusing addendum

    If you're truly sure you want to get the script to expand the *, you have to make sure that * is passed to the script (enclosed in quotes, as in the other answers), and then make sure it is expanded at the right point in the processing (which is not trivial). At that point, I'd use an array.

    names=( $@ )
    for file in "${names[@]}"
    do
        echo "$file"
    done
    

    I don't often use $@ without the double quotes, but this is one time when it is more or less the correct thing to do. The tricky part is that it won't handle wild cards with spaces in very well.

    Consider:

    $ > "double  space.c"
    $ > "double  space.h"
    $ echo double\ \ space.?
    double  space.c double  space.h
    $
    

    That works fine. But try passing that as a wild-card to the script and ... well, let's just say it gets to be tricky at that point.

    If you want to extract $2 separately, then you can use:

    names=( $1 )
    for file in "${names[@]}"
    do
        echo "$file"
    done
    # ... use $2 ...
    

提交回复
热议问题