Loop through filenames with spaces within a path

前端 未结 1 1154
南笙
南笙 2021-01-23 07:58

I wanted to pass the path name to the script, but my path has spaces (eg. /Users/netto/iTunes \\Media/Music/). I have tried putting the actual path directly on the for loop, and

相关标签:
1条回答
  • 2021-01-23 08:33

    Spaces in variables are expanded at command level, so to solve the problem you can either put quotation marks around "$1" (so spaces are properly escaped) or use bash arrays.

    Here is two example that should work:

     #!/bin/bash
    DIR="$1"
    for f in "$DIR"/* 
    do
      echo "Processing file $f " 
    done
    

    or using bash arrays:

    #!/bin/bash
    FILES=("$1"*)
    for f in "${FILES[@]}"
    do
      echo "Processing file $f " 
    done
    
    0 讨论(0)
提交回复
热议问题