在文件名中调用时,Shell变量为空,但在echo或在其他地方调用时则不为空

时间:2013-09-29 06:50:04

标签: linux bash shell variables

我设置了4个shell变量

remote_account="raccname"
remote_machine="server"
First_Name="firstname"
Last_Name="lastname"

当我在我的脚本中调用remote_accountremote_machine时,它们会得到很好的使用

echo "What directory shall you choose?"
  read dir
  scp -r ~/csc60/$dir $remote_account@$remote_machine:directoryA
  exit;;

但是当我把其他两个称为

echo "What directory shall you choose?"
  read dir
  scp $remote_account@$remote_machine:tars/$Last_Name_$First_Name_$dir.tar.z ~/tars
  exit;;

它从tars/$dir.tar.z抓取tars文件,完全跳过$Last_Name_$First_Name_ 当我在其中抛出echo $Last_Name时仍将其显示为“姓氏”

在变量之间是否有一些使用“_”的规则,或者我只是遗漏了一些明显的东西?

2 个答案:

答案 0 :(得分:5)

_是变量名称的有效字符,因此您必须限定哪个部分是变量。

scp "$remote_account@$remote_machine:tars/${Last_Name}_${First_Name}_$dir.tar.z" ~/tars

答案 1 :(得分:0)

您需要使用$ {}来界定变量值,因为您声明$ Last_Name_ $ First_Name_ $ dir被视为一个变量和shell put值。在您的情况下,您没有为它定义值。它将被解释为“”。

使用${LAST_NAME}_${FIRST_NAME}