Fusion
Fusion

Reputation: 5472

Unix env path variable with wave sign

on my Ubuntu 16.04.4 LTS, xenial, I wanted to declare environment variable, which contains a path to my ~/.virtualenvs folder.

What I want:

To have an system-wide environment string variable, which I can use in my unix commands, for example cd $MYVAR or in similar elegant usage.

What I tried:

I added following line into /etc/environment file and then restarted server:

$PATH_VENV="~/.virtualenvs"

Problem:

When I try to use my newly declared variable within unix commands, this is an output:

cd ${PATH_VENV}
-bash: cd: ~/.virtualenvs: No such file or directory

I think UNIX literally interprets ~/.virtualenvs as name of folder, instead interpretting ~ as home folder. How to solve this? Is there any workaround?

Wierd Hint:

I inspected my $PATH_VENV variable like follows, and found out, there is an additional newline, However I have no idea why:

echo $PATH_VENV | od -a
0000000   ~   /   .   v   i   r   t   u   a   l   e   n   v   s  nl
0000017

Upvotes: 1

Views: 285

Answers (1)

Kurtis Rader
Kurtis Rader

Reputation: 7459

First, your /etc/environment file should not contain a dollar-sign before the var name. Second, POSIX 1003 shells like bash perform tilde expansion before parameter expansion. If you type man bash read the section named EXPANSION. The problem is that /etc/environment is read (on Linux at least) by the pam_env module which does not perform tilde expansion. If you run env | grep PATH_VENV you will see that the tilde has not been replaced with your home directory. And because POSIX 1003 shells perform tilde expansion before variable expansion you end up doing the equivalent of cd '~/.virtualenvs' (note the quotes around the path).

The answer is to not set this var in /etc/environment. Set it in your ~/.profile or ~/.bashrc.

P.S., The "additional newline" is because you used echo which, by default, appends a newline to the string it echoes.

P.P.S., What you called a "wave" sign is normally called the "tilde" character.

Upvotes: 1

Related Questions