for name in `ls` and filenames with spaces

后端 未结 3 1070
予麋鹿
予麋鹿 2020-12-01 10:43

next code doesnt work because of spaces in file names, How to fix?

IFS = \'\\n\'
for name in `ls `
do
    number=`echo \"$name\" | grep -o \"[0-9]\\{1,2\\}\"         


        
3条回答
  •  离开以前
    2020-12-01 11:26

    Replace

    for name in `ls`
    

    with:

    ls | while read name
    

    Notice: bash variable scoping is awful. If you change a variable inside the loop, it won't take effect outside the loop (in my version it won't, in your version it will). In this example, it doesn't matter.

    Notice 2: This works for file names with spaces, but fails for some other strange but valid file names. See Charles Duffy's comment below.

提交回复
热议问题