0

The following tilde expansion works as expected.

$ A=~/foo.txt
$ echo $A
/home/lone/foo.txt

In the following case, tilde expansion does not work because the tildes are within quotes.

$ A="~/foo.txt ~/bar.txt"
$ echo $A
~/foo.txt ~/bar.txt

I know I can do the following instead because environment variables expand within quotes.

$ A="$HOME/foo.txt $HOME/bar.txt"
$ echo $A
/home/lone/foo.txt /home/lone/bar.txt

But is there a way to fix the second example above so that the tilde expansion works while setting the environment variable?

I tried something like this but it didn't solve the entire problem.

$ A=~/foo.txt" "~/bar.txt
$ echo $A
/home/lone/foo.txt ~/bar.txt

What else can I do?

Note: The solution should work for any POSIX shell.

Lone Learner
  • 12,094
  • 14
  • 66
  • 159

1 Answers1

0

try this:

eval echo $A

Output:

$ A="~/foo.txt ~/bar.txt"
$ echo $A
~/foo.txt ~/bar.txt
$ eval echo $A
/home/oracle/foo.txt /home/oracle/bar.txt
MaxU
  • 173,524
  • 24
  • 290
  • 329