Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tilde expansion without eval

Tags:

bash

I need to get user home folder. At the moment I'm using this

home=$(eval echo ~$user)

Content of $user is provided by the caller, so not trusted value. Is the use of the eval dangerous here? If yes (probably), how to solve it without it?

like image 891
graywolf Avatar asked Dec 15 '25 15:12

graywolf


1 Answers

On systems that have it, you can use getent(1):

$ getent passwd "$username" | cut -d: -f 6
/path/to/user/home

Make sure to check for empty results though.

like image 136
Ignacio Vazquez-Abrams Avatar answered Dec 17 '25 05:12

Ignacio Vazquez-Abrams