Handle special characters in bash for…in loop

前端 未结 3 964
轮回少年
轮回少年 2021-01-13 13:16

Suppose I\'ve got a list of files

file1
\"file 1\"
file2

a for...in loop breaks it up between whitespace, not newlines:

for         


        
3条回答
  •  我在风中等你
    2021-01-13 13:58

    Actually, Mark's suggestion works fine without even doing anything to the internal field separator. The problem is running ls in a subshell, whether by backticks or $( ) causes the for loop to be unable to distinguish between spaces in names. Simply using

    for f in *
    

    instead of the ls solves the problem.

    #!/bin/bash
    for f in *
    do
     echo "$f"
    done
    

提交回复
热议问题