bash: passing paths with spaces as parameters?

前端 未结 4 898
北海茫月
北海茫月 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条回答
  •  逝去的感伤
    2021-02-12 22:56

    #! /bin/bash
    
    for fname in "$@"; do
      process-one-file-at-a-time "$fname"
    done
    

    Note the excessive use of quotes. It's all necessary.

    Passing all the arguments to another program is even simpler:

    process-all-together "$@"
    

    The tricky case is when you want to split the arguments in half. That requires a lot more code in a simple POSIX shell. But maybe the Bash has some special features.

提交回复
热议问题