linux - 在文件名中调用时 Shell 变量为空,但在回显或在其他地方调用时则不是

标签 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 时,它仍然显示为“lastname”

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

最佳答案

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

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

关于linux - 在文件名中调用时 Shell 变量为空,但在回显或在其他地方调用时则不是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19075286/

相关文章:

linux - 如何恢复被覆盖的 bash 配置文件

python - 读取-未知选项

php - 如何使用 PHP 进行文本 DIFF?

linux - 从下面的 Shell 脚本中查找哪个文件有问题

bash - 无法在 shell 脚本中转义引号

javascript - 找不到 PhantomJS 的故障转储

linux - 如何查找具有特定名称的所有子目录并删除其内容(而不是删除目录本身)

linux - 在 Linux 上为 gtkmm 和交叉编译设置 OpenGL

java - 检测新的网络连接(linux-server)及其在 java 中的状态

bash - bash 脚本中的并发日志记录