Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current username in Julia (Linux)

Tags:

linux

julia

I must be doing something stupid, but I can't seem to retrieve the current username using Julia. The closest function in Base appears to be gethostname(), but that returns the computer name, not the username. I tried system calls but am having trouble due to the interpolation character $. Specifically, although echo $USER returns the appropriate username in a terminal, when I try the following in Julia I get various errors or incorrect answers:

run(`echo $USER`)
run(`echo "$USER"`)
run(`echo '$USER'`)
run(`echo '$'USER`)
run(`echo \$USER`)

I guess the issue is that Julia is misinterpreting the $ as an interpolation, but I've got no idea how to get around this.

Any ideas?

like image 930
Colin T Bowers Avatar asked Jan 07 '15 00:01

Colin T Bowers


2 Answers

An easy workaround:

run(`whoami`)

But unnecessary, as this works:

ENV["USER"]
like image 67
Amadan Avatar answered Oct 20 '22 21:10

Amadan


This works on both Linux and Windows:

splitdir(homedir())[end]
like image 41
Iskander Avatar answered Oct 20 '22 21:10

Iskander