0

I have environment variable export MY_WORK_DIR="~/project". While I'm using below command, it give me an error:

realpath $MY_WORK_DIR

realpath: '~/project': No such file or directory

In my guess, the ~ is not processed while using this env variable.

BTW, export MY_WORK_DIR=~/project is not an option for me. ~ should be in the string.

Could you please guide me how to get real path from envrionment variable ~/project ?

EDIT

Sorry. The variable is from other app so I cannot modify the environment variable which contains tilde. (Storing variable with tilde expanded form is not an option).

EDIT2

Is it safe to use eval command like this? eval "echo ${MY_WORK_DIR}". It works for my use.

Dorr
  • 577
  • 1
  • 8
  • 22
  • Sorry. The variable is from other app so I cannot modify the environment variable which contains tilde. (Storing variable with tilde expanded form is not an option). I just want to know how can I properly get the real path from tilde included variable. – Dorr May 13 '20 at 04:23
  • Okay, I have reopened the question. – Cyrus May 13 '20 at 04:25
  • Oh. I tried eval command. It works as expected. Is it safe to use eval command? eval "echo ${MY_WORK_DIR}" – Dorr May 13 '20 at 04:34
  • 1
    If you cannot modify the env variable then your option is only `eval`, I'm afraid. `eval realpath "$MY_WORK_DIR"`, eval is not safe since it can expand/execute arbitrary commands in a variable (shell injection 101) so use it at your own risks... – Jetchisel May 13 '20 at 04:49

2 Answers2

0

Following steps can provide a resolution:

  1. You need to replace "~" with the full path of the project directory.
  2. Use pwd command to identify the full path of the project directory; e.g. /root/Documents/project is the full path you get.
  3. Execute this command export MY_WORK_PROJECT=/root/Documents/project
  4. Execute this command echo $MY_WORK_PROJECT so you should get this result /root/Documents/project
Usama Abdulrehman
  • 1,041
  • 3
  • 11
  • 21
  • For step 2: Why not use `$PWD` instead? – user1934428 May 13 '20 at 07:03
  • The output path resulting from executing `PWD` or `$PWD` is same which is in this case `/root/Documents/project` – Usama Abdulrehman May 13 '20 at 20:02
  • The effect is the same, but you create a child process unnecessarily. This may matter, if the code in question is executed repeatedly in a loop and is the reason why I would go for ``$PWD. OTOH, `$(pwd)` is **always correct**, while `$PWD` may "lie" in the pathological case that someone had assigned explicitly `PWD=....` before. At least it might make sense presenting both alternatives and explaining the pros and cons, so the OP can understand the difference and choose as he likes. – user1934428 May 14 '20 at 07:13
0

I wouldn't use eval if I can avoid it. Especially in the way you are doing it, this is an invitation to do havoc by embedding dangerous code into MY_WORK_DIR.

A cheap solution for your concrete example would be to do a

if [[ ${MY_WORK_DIR:0:1} == '~' ]]
then 
  MY_WORK_DIR="$HOME/${MY_WORK_DIR:1}"
fi

which chops off the annoying ~ and prepends your home directory. But this would fail if MY_WORK_DIR is set to, say, ~einstein/project.

In this case, you would have to extract the user name (einstein) and search the home directory for this user.

user1934428
  • 19,864
  • 7
  • 42
  • 87