在包含空格的Bash脚本中传递多个参数

时间:2018-02-06 21:07:27

标签: linux bash shell

我刚开始学习Bash Script。

我的脚本接受1到n个参数。每个参数都传递给函数重命名。 我的问题是我传递的参数不接受空格。

script.sh

#!/bin/bash
for FILE in $@
do
    echo "$FILE"
    rename $FILE
done

对于Ex:

./script.sh /Users/xyz/Section 5/abc/ /Users/xyz/pqr/ /Users/z/abc

以上论点" / Users / xyz / Section 5 / abc /"即使它包含空间也应该是一个。 但是script.sh中的代码会将其分解为两个参数。

So the output is:

/Users/xyz/Section
5/abc/
/Users/xyz/pqr/
/Users/z/abc

But My Expected Output should be:

/Users/xyz/Section 5/abc/
/Users/xyz/pqr/
/Users/z/abc

注意:我尝试过不同的解决方案:

1)" / Users / xyz / Section 5 / abc /" - >相同的输出,即2个不同的参数

2)' / Users / xyz / Section 5 / abc /' - >相同的输出,即2个不同的参数

3)/ Users / xyz / Section \ 5 / abc / - >相同的输出,即2个不同的参数

0 个答案:

没有答案
相关问题