传递bash变量不起作用

时间:2016-02-14 15:09:19

标签: bash

我在bash中有以下设置:

 export chain_dir='~/.eris/chains/simplechain'

这有效

 echo $chain_dir
 ~/.eris/chains/simplechain

然后尝试我尝试与ls一起使用它,我得到:

 ls $chain_dir
 ls: cannot access ~/.eris/chains/simplechain: No such file or directory

但是剪切并粘贴与$ chain_dir中相同的目录字符串,它可以工作:

 ~/.eris/chains/simplechain
 accounts.csv  addresses.csv  simplechain_full_000  simplechain_root_000 

我相信我错过了一些东西:

3 个答案:

答案 0 :(得分:0)

~在传递给ls之前,不会扩展到您的主目录路径。

我建议您完整地写出来,/home/user/或使用$HOME

export chain_dir="$HOME/.eris/chains/simplechain"

请注意在字符串周围使用双引号。

答案 1 :(得分:0)

在将~提交给ls之前,

$HOME不会扩展到您的主目录。

使用eval

$var1='~/.config/'为您进行扩展    $ls $(eval echo "$var1")
   let CloseButton: UIButton = UIButton(type: UIButtonType.Custom) CloseButton.frame = CGRectMake(0, 0, 24, 24) CloseButton.setImage(UIImage(named:"close"), forState: UIControlState.Normal) CloseButton.addTarget(self, action: "close", forControlEvents: UIControlEvents.TouchUpInside) let leftBarCloseButton: UIBarButtonItem = UIBarButtonItem(customView: CloseButton) let EditButton: UIButton = UIButton(type: UIButtonType.Custom) EditButton.frame = CGRectMake(0, 0, 24, 24) EditButton.setImage(UIImage(named:"edit"), forState: UIControlState.Normal) EditButton.addTarget(self, action: "edit", forControlEvents: UIControlEvents.TouchUpInside) let leftBarEditButton: UIBarButtonItem = UIBarButtonItem(customView: EditButton) self.navigationItem.setLeftBarButtonItems([leftBarCloseButton,leftBarEditButton], animated: true)

答案 2 :(得分:0)

~因为您引用它而未被扩展。将其保留在引号之外,bash将在执行赋值之前展开它。请注意,只要~在引号之外,您就可以引用路径的 rest

$ chain_dir=~'/.eris/chains/simplechain'
$ echo "$chain_dir"
/home/tbrooke/.eris/chains/simplechain

(假设/home/tbrooke是您的主目录。)