File names with spaces in BASH

前端 未结 7 1373
清酒与你
清酒与你 2020-12-03 07:54

I\'m trying to write script that\'ll crop and resize large photos into HD Wallpapers.

#! /bin/bash


for i in `ls *.jpg`
do
    width=`identify -format \'%w         


        
7条回答
  •  旧巷少年郎
    2020-12-03 08:32

    I would recommend to write the for-line like this:

    for i in *.jpg
    

    and encapsulate $i in double-quotes: "$i".


    If you insist on the

    `ls *.jpg`
    

    style, (if you for instance get your file-names from a more complex command) you could try setting IFS to \n:

    IFS='\n'
    

    Compare these two executions:

    $ for f in `ls *`; do echo $f; done
    hello
    world
    test
    
    $ IFS='\n'; for f in `ls *`; do echo $f; done
    hello world
    test
    

提交回复
热议问题