1

I have the following set in bash:

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

This works

 echo $chain_dir
 ~/.eris/chains/simplechain

Then try I try to use it with ls and I get:

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

But cut and paste the same directory string as in $chain_dir and it works:

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

I believe that I am missing something:

tbrooke
  • 2,137
  • 2
  • 17
  • 25

3 Answers3

0

The ~ isn't expanded to your home directory path before being passed to ls.

I would suggest writing it out in full, /home/user/, or using $HOME:

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

Note the use of double quotes around the string.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
0

~ isnt expanded into your home dir before giving it to ls.

Use $HOME or

leteval do the expansion for you
$var1='~/.config/'
$ls $(eval echo "$var1")

riteshtch
  • 8,629
  • 4
  • 25
  • 38
  • There's *really* no need for `eval`; `~` is expanded if it's the first character of the assigned value and not quoted: `var1=~/.config`. – chepner Feb 14 '16 at 16:19
0

~ isn't being expanded because you quoted it. Leave it outside the quotes, and bash will expand it before performing the assignment. Notice that you can quote the rest of the path if necessary, as long as the ~ is outside the quotes.

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

(Assuming /home/tbrooke is your home directory.)

chepner
  • 497,756
  • 71
  • 530
  • 681