我有以下文件:〜/ tmp/testbash $ l file 1.test move.sh*
where move.sh is:
#!/bin/bash
#-x
FILENAME='file\ .test'
echo $FILENAME
echo joo
mv $FILENAME test.test
Run Code Online (Sandbox Code Playgroud)
当我运行./move.sh时,我收到此输出和错误:
file\ .test
joo
mv: target `test.test' is not a directory
Run Code Online (Sandbox Code Playgroud)
问题是它执行命令为:mv file .test test.test而不是:mv file\.test test.test
有什么建议来解决这个问题
如果变量包含嵌入的空格,则将变量括在双引号(")中.
FILENAME='file .test'
mv "$FILENAME" test.test
Run Code Online (Sandbox Code Playgroud)
如果变量值内有空格,请使用双引号:
FILENAME='file\ .test'
echo "$FILENAME"
echo joo
mv "$FILENAME" test.test
Run Code Online (Sandbox Code Playgroud)