Bash glob parameter only shows first file instead of all files

后端 未结 4 778
遇见更好的自我
遇见更好的自我 2020-12-12 08:36

I want to run this cmd line script

$ script.sh   lib/* ../test_git_thing

I want it to process all the files in the /lib folder.

<         


        
4条回答
  •  一个人的身影
    2020-12-12 09:04

    As correctly noted, lib/* on the command line is being expanded into all files in lib. To prevent expansion, you have 2 options. (1) quote your input:

    $ script.sh 'lib/*' ../test_git_thing
    

    Or (2), turn file globbing off. However, the option set -f will disable pathname expansion within the shell, but it will disable all pathname expansion (setting it within the script doesn't help as expansion is done by the shell before passing arguments to your script). In your case, it is probably better to quote the input or pass the first arguments as a directory name, and add the expansion in the script:

    DIR=$1
    for f in "$DIR"/*
    

提交回复
热议问题