# Can we access shell variables from Julia?

**URL:** <https://discourse.julialang.org/t/can-we-access-shell-variables-from-julia/11750>\
**Category:** General Usage\
**Tags:** shell\
**Created:** [June 18, 2018, 8:15am UTC](https://discourse.julialang.org/t/can-we-access-shell-variables-from-julia/11750 "2018-06-18T08:15:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [June 18, 2018, 8:15am UTC](https://discourse.julialang.org/t/can-we-access-shell-variables-from-julia/11750/1 "2018-06-18T08:15:31Z")

</div>

I may be missing something but would it be possible to use `$` (or appropriate symbol) in a `Cmd` object in the “shell sense” as opposed to the julia interpolation sense? As a slightly stupid example:

```julia
run(`echo $PWD`)

```

would return the path. Of course that would be useless and `pwd()` is the obvious choice but it’s just an analogy. Executing the snippet above gives an error because `PWD` is not defined in the environment. Escaping the `$` sign with single quotes or `\` just shows `"$PWD"`. Is it by design that we can’t use `$` in another way than the string interpolation?

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [June 18, 2018, 8:22am UTC](https://discourse.julialang.org/t/can-we-access-shell-variables-from-julia/11750/2 "2018-06-18T08:22:50Z")

</div>

```julia
julia> run(`echo $(ENV["PWD"])`)
/home/tbesard

```

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [June 18, 2018, 8:39am UTC](https://discourse.julialang.org/t/can-we-access-shell-variables-from-julia/11750/4 "2018-06-18T08:39:54Z")

</div>

Cool yes that works and answers the question, thanks!

Unfortunately, one of the commands I’m trying to run is `echo $!` (last process) which is not in `ENV`. I ended up finding a workaround using a `pipeline` with a bunch of `grep`s though, but it’s a bit of a hack…

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [June 18, 2018, 1:15pm UTC](https://discourse.julialang.org/t/can-we-access-shell-variables-from-julia/11750/5 "2018-06-18T13:15:44Z")

</div>

> [@tlienart](#):
>
> `echo $!` (last process) which is not in `ENV`

Because `$!` is a special variable as implemented by your shell, which isn’t used by `run` (see docs, “The command is never run with a shell”). You can use it if you explicitly launch a shell:

```julia
julia> run(`bash -c "echo & echo \$!"`)
4185

```

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [June 19, 2018, 12:37am UTC](https://discourse.julialang.org/t/can-we-access-shell-variables-from-julia/11750/6 "2018-06-19T00:37:15Z")

</div>

very useful, thanks!
