【问题标题】:how to set up an environment variable as partial path that contains space with bash如何将环境变量设置为包含 bash 空格的部分路径
【发布时间】:2020-08-26 03:43:17
【问题描述】:

我的路径很长 /mnt/ABC/user/user123/domain1/computer1/My Document/ 里面有多个目录。我试图输入完整路径,并考虑在 .profile 中设置一个环境变量来缩短它,但无论如何都失败了。请帮忙。

attempt 1:
adm@linux-lygy:~> export XYZS=/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ Software
adm@linux-lygy:~> echo $XYZS/DB
/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ/DB

attempt 2:
adm@linux-lygy:~> export XYZS="/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ Software"
adm@linux-lygy:~> echo $XYZS/DB
/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ Software/DB
adm@linux-lygy:~> ls $XYZS/DB
ls: cannot access '/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ': No such file or directory
ls: cannot access 'Software/DB': No such file or directory

attempt 3:
adm@linux-lygy:~> export XYZS="/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ\ Software"
adm@linux-lygy:~> echo $XYZS/DB
/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ Software/DB
adm@linux-lygy:~> echo $XYZS/DB
/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ\ Software/DB
adm@linux-lygy:~> ls $XYZS/DB
ls: cannot access '/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ\': No such file or directory
ls: cannot access 'Software/DB': No such file or directory

attempt 4:
adm@linux-lygy:~> export XYZS="\"/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ Software\""
adm@linux-lygy:~> echo $XYZS/DB
"/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ Software"/DB
adm@linux-lygy:~> ls $XYZS/DB
ls: cannot access '"/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ': No such file or directory
ls: cannot access 'Software"/DB': No such file or directory
adm@linux-lygy:~> echo "$XYZS"/DB
"/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ Software"/DB
adm@linux-lygy:~> ls "$XYZS"/DB
ls: cannot access '"/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ Software"/DB': No such file or directory

attempt 5:
adm@linux-lygy:~> export XYZS="\"/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ\ Software\""
adm@linux-lygy:~> echo $XYZS/DB
"/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ\ Software"/DB
adm@linux-lygy:~> ls $XYZS/DB
ls: cannot access '"/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ\': No such file or directory
ls: cannot access 'Software"/DB': No such file or directory
adm@linux-lygy:~> echo "$XYZS"/DB
"/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ\ Software"/DB
adm@linux-lygy:~> ls "$XYZS"/DB
ls: cannot access '"/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ\ Software"/DB': No such file or directory

【问题讨论】:

  • “我试图输入完整路径”——你的意思是“我厌倦了输入完整路径”?无论如何,请阅读 shell 的说明。例如,对于 Bash,control-R 允许您对历史记录进行反向搜索。在任何情况下,如果您有空格,则必须使用引号来防止 shell 在该空格处将命令行拆分为两个参数。

标签: bash unix environment-variables


【解决方案1】:

你的第二次尝试是正确的:

export XYZS="/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ Software"

问题在于您如何使用该变量。您需要引用它,因为它包含空格:

echo "$XYZS/DB"
ls "$XYZS/DB"

你应该quote variables as a rule of thumb。有关详细信息,请参阅 Shellcheck wiki:Double quote to prevent globbing and word splitting

【讨论】:

  • 知道了,谢谢。我知道我应该引用变量,但不知道我应该在引号中包含以下路径。
  • @MeOkey 实际上,引用整个内容只是一种约定。如果您使用"$XYZS"/DB,它也会起作用。在您的尝试 4 和 5 中,变量的值错误,因此引用与问题无关。欢迎顺便说一句!
  • @MeOkey :您可以考虑切换到 Zsh 作为交互式 shell,在这种情况下不需要引用。
  • @user1934428 同样适用于 Fish
猜你喜欢
  • 2011-07-15
  • 2013-07-26
  • 2010-09-18
  • 1970-01-01
  • 1970-01-01
  • 2018-01-04
  • 2017-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多