bash - 如何使用包含空格的变量?

标签 bash

我有以下文件:

~/tmp/testbash$ l
file 1.test  move.sh*

move.sh 是:

#!/bin/bash
#-x

FILENAME='file\ .test'
echo $FILENAME
echo joo
mv $FILENAME test.test

当我运行 ./move.sh 时,我得到了这个输出和错误:

file\ .test
joo
mv: target `test.test' is not a directory

问题是它执行的命令是:

mv file .test test.test

而不是:

mv file\ .test test.test

我该如何解决这个问题?

最佳答案

如果变量包含嵌入空格,则用双引号 (") 将变量括起来。

FILENAME='file .test'
mv "$FILENAME" test.test

关于bash - 如何使用包含空格的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10847127/

相关文章:

linux - 错误 Shell 脚本

bash - 在 Bash 中生成符合范围的随机数

linux - shell 中简单 bash 命令的问题

linux - 摆脱 shell 脚本中的 tail -f

mysql - 替代 MySQL LOAD LOCAL INFILE REPLACE 更新表

linux - bash 中 expr 的语法错误

linux - 如果从批处理脚本调用 bash 终端,如何打印它的输出?

bash - 提取部分代码并在 bash 中解析 HTML

html - bash 脚本 sed 用变量计数替换图像编号

linux - 根据名称杀死早于某一天的进程