bash: passing paths with spaces as parameters?

前端 未结 4 900
北海茫月
北海茫月 2021-02-12 22:14

I have a bash script that recieves a set of files from the user. These files are sometimes under directories with spaces in their names. Unfortunately unlike this question all t

4条回答
  •  梦毁少年i
    2021-02-12 22:50

    You want "$@", which has the special syntax of expanding $@ but preserving the white-space quoting of the caller (it does not create a single giant string with all the arguments in it). So someone can call your script like:

    bash-script.sh AFile "Another File With Spaces"
    

    Then in your script you can do things like:

    for f in "$@"; do 
      echo "$f"; 
    done
    

    and get two lines of output (not 5).

    Read the paragraph about the Special Parameter "@" here: http://www.gnu.org/s/bash/manual/bash.html#Special-Parameters

提交回复
热议问题