如何使用包含空格的变量? [英] How can I use a variable that contains a space?

查看:95
本文介绍了如何使用包含空格的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下文件:

~/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时,得到以下输出和错误:

When I run ./move.sh, I get this output and error:

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

问题在于它以以下方式执行命令:

The problem is that it executes the command as:

mv file .test test.test

而不是:

mv file\ .test test.test

我该如何解决?

推荐答案

如果变量值中有空格,请使用双引号:

Use double quotes if you have spaces inside a variable value:

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

这篇关于如何使用包含空格的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆