# How to get username?

**URL:** https://discourse.julialang.org/t/how-to-get-username/93023
**Category:** General Usage
**Created:** [January 16, 2023, 11:59am UTC](https://discourse.julialang.org/t/how-to-get-username/93023 "2023-01-16T11:59:12Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![tp2750](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tp2750/32/207806_2.png) [@tp2750](https://discourse.julialang.org/u/tp2750)
#### Post date: [January 16, 2023, 11:59am UTC](https://discourse.julialang.org/t/how-to-get-username/93023/1 "2023-01-16T11:59:12Z")

</div>

How do I get the username of the user running the julia process?

On Linux I can do

```julia
ENV["USER"]

```

and on Windows:

```julia
ENV["USERNAME"]

```

But I don’t know what to do on other operating systems.

What is the os-independent way to ask for the username?

---

<div class="post-metadata">

### Author: ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)
#### Post date: [January 16, 2023, 12:10pm UTC](https://discourse.julialang.org/t/how-to-get-username/93023/2 "2023-01-16T12:10:45Z")

</div>

run in Julia REPL

```julia
run(`sh -c 'echo $USER'`)

```

I can’t do `ENV["USER"]`

to obtain username in my OS I need to `echo $USER`

![Capture d’écran_2023-01-16_19-11-47](https://global.discourse-cdn.com/julialang/original/3X/8/c/8c42c23a6deb65748680df0c43d25f0688e69fb9.png)

![Capture d’écran_2023-01-16_19-13-11](https://global.discourse-cdn.com/julialang/original/3X/9/1/91a909095124cad967cb7665677c29ebfd2ae281.png)

---

<div class="post-metadata">

### Author: ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)
#### Post date: [January 16, 2023, 12:14pm UTC](https://discourse.julialang.org/t/how-to-get-username/93023/3 "2023-01-16T12:14:06Z")

</div>

I am not sure if something like this is available. Python has [`getpass.getuser`](https://docs.python.org/3/library/getpass.html#getpass.getuser), which only checks the environment variables `LOGNAME`, `USER`, `LNAME`, and `USERNAME`. So probably you could use something like

```julia
function username()
    varnames = ["LOGNAME", "USER", "LNAME", "USERNAME"]
    for varname in varnames
        haskey(ENV, varname) && return ENV[varname]
    end
    return nothing
end

```

---

<div class="post-metadata">

### Author: ![tp2750](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tp2750/32/207806_2.png) [@tp2750](https://discourse.julialang.org/u/tp2750)
#### Post date: [January 16, 2023, 12:25pm UTC](https://discourse.julialang.org/t/how-to-get-username/93023/4 "2023-01-16T12:25:19Z")

</div>

Thank you @barucden .

If it’s good enough for python it is good enough for me 😄

I’m just a bit surprised there isn’t a standard function in Julia to get this.  
When we have `homedir()` I would expect we also had `username()`.

---

<div class="post-metadata">

### Author: ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)
#### Post date: [January 16, 2023, 12:37pm UTC](https://discourse.julialang.org/t/how-to-get-username/93023/5 "2023-01-16T12:37:08Z")

</div>

The reason we don’t have such a function might be that nobody requested it so far. If you think it should be in Julia base, then consider creating an issue.

To be precise, the python function does more than what I sketched:

> If none are set, the login name from the password database is returned on systems which support the [`pwd`](https://docs.python.org/3/library/pwd.html#module-pwd) module, otherwise, an exception is raised.

Our `homedir()` function is based on libuv, which also seems to provide _[a way](http://docs.libuv.org/en/v1.x/misc.html#c.uv_os_get_passwd)_ to get a username from passwd. It shouldn’t be too hard to basically mirror python’s functionality (i.e., try the environment variables and potentially resort to passwd).

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [January 16, 2023, 1:24pm UTC](https://discourse.julialang.org/t/how-to-get-username/93023/6 "2023-01-16T13:24:35Z")

</div>

> [@barucden](#):
>
> Our `homedir()` function is based on libuv, which also seems to provide _[a way](http://docs.libuv.org/en/v1.x/misc.html#c.uv_os_get_passwd)_ to get a username from passwd. It shouldn’t be too hard to basically mirror python’s functionality (i.e., try the environment variables and potentially resort to passwd).

Something like:

```julia
struct uv_passwd
    username::Cstring
    uid::Clong
    gid::Clong
    shell::Cstring
    homedir::Cstring
end

function username()
    for varname in ("LOGNAME", "USER", "LNAME", "USERNAME")
        username = get(ENV, varname, "")
        !isempty(username) && return username
    end
    p = Ref{uv_passwd}()
    ret = @ccall uv_os_get_passwd(p::Ref{uv_passwd})::Cint
    Base.uv_error("get_passwd", ret)
    username = unsafe_string(p[].username)
    @ccall uv_os_free_passwd(p::Ref{uv_passwd})::Cvoid
    return username
end

```

Note also that you should check whether the environment variables are **non-empty** , not just whether they exist. This is [what Python does](https://github.com/python/cpython/blob/f4c03484da59049eb62a9bf7777b963e2267d187/Lib/getpass.py#L154-L169).

_Update_: Alternatively, Julia’s Libc module apparently already contains an [undocumented function](https://github.com/JuliaLang/julia/blob/74fc310748291073b6534d6fbc267ab8c8b4b599/base/libc.jl#L438-L456) for this, though it doesn’t check the environment variables first:

```julia
julia> Libc.getpwuid(Libc.getuid(), true)
Base.Libc.Passwd("stevenj", 0x00000000000001f5, 0x0000000000000014, "/bin/zsh", "/Users/stevenj", "Steven G. Johnson")

julia> Libc.getpwuid(Libc.getuid(), true).username
"stevenj"

```

> [@tp2750](#):
>
> I’m just a bit surprised there isn’t a standard function in Julia to get this.  
> When we have `homedir()` I would expect we also had `username()`.

I filed an issue: [username() function (analogous to homedir()) · Issue #48302 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/48302)

---

<div class="post-metadata">

### Author: ![tp2750](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tp2750/32/207806_2.png) [@tp2750](https://discourse.julialang.org/u/tp2750)
#### Post date: [January 16, 2023, 2:52pm UTC](https://discourse.julialang.org/t/how-to-get-username/93023/7 "2023-01-16T14:52:02Z")

</div>

Thank you very much @stevengj .

Looks like this can go right into a PR 😄
