Search This Blog

Shell script: for loop through file names with spaces

  • Use find:
    find . -print0 | while read -d $'\0' f
    do
      cat "$f"
    done
    
  • Use IFS:
    #!/bin/bash
    IFS_BAKCUP=$IFS
    
    IFS=$(echo -en "\n\b")
    
    # $1 is the directory contains the files
    for f in $(ls $1)
    do
      echo "$f"
    done
    
    IFS=$IFS_BACKUP
    
  • Use Array:

See also

No comments:

Post a Comment