# What makes bash forget the environment variable?

**URL:** <https://discourse.julialang.org/t/what-makes-bash-forget-the-environment-variable/87821>\
**Category:** General Usage\
**Created:** [September 26, 2022, 4:04pm UTC](https://discourse.julialang.org/t/what-makes-bash-forget-the-environment-variable/87821 "2022-09-26T16:04:58Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [September 26, 2022, 4:04pm UTC](https://discourse.julialang.org/t/what-makes-bash-forget-the-environment-variable/87821/1 "2022-09-26T16:04:58Z")

</div>

My bash knows what PATH is:

```julia
pkonl@Hedwig MINGW64 ~/ (main)$ echo $PATH                                                                                             
/mingw64/bin:/usr/bin....           
                  

```

However, starting a julia somehow makes the bash lose that information:

```julia
pkonl@Hedwig MINGW64 ~/ (main)$ julia                                                                                                  
              _                                                                                         
   _ _ _(_)_ | Documentation: https://docs.julialang.org                                      
  (_) | (_) (_) |                                                                                 
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.                                          
  | | | | | | |/ _` | |                                                                                 
  | | |_| | | | (_| | | Version 1.8.1 (2022-09-06)                                                     
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release                                        
|__/ |                                                                                 
                                                                                                         
julia> run(`echo $PATH`)                                                                                 
ERROR: UndefVarError: PATH not defined                                                                   
Stacktrace:                                                                                              
 [1] top-level scope                                                                                     
   @ REPL[1]:1                                                                                           
                                                                                                         

```

Does anyone know why?

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [September 26, 2022, 4:12pm UTC](https://discourse.julialang.org/t/what-makes-bash-forget-the-environment-variable/87821/2 "2022-09-26T16:12:55Z")

</div>

Julia is trying to interpolate the value of a Julia variable PATH

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 26, 2022, 4:13pm UTC](https://discourse.julialang.org/t/what-makes-bash-forget-the-environment-variable/87821/3 "2022-09-26T16:13:20Z")

</div>

Thats not bash, which produces the error but Julia trying to interpolate PATH:

```julia
julia> run(`echo $PATH`)
ERROR: UndefVarError: PATH not defined
Stacktrace:
 [1] top-level scope
   @ REPL[1]:1

julia> PATH="XXX"
"XXX"

julia> run(`echo $PATH`)
XXX
←[0mProcess(`←[4mecho←[24m ←[4mXXX←[24m`, ProcessExited(0))

```

(ninjad) 🙂

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 26, 2022, 4:16pm UTC](https://discourse.julialang.org/t/what-makes-bash-forget-the-environment-variable/87821/4 "2022-09-26T16:16:02Z")

</div>

What you want to see or test is this:

```julia
julia> run(`bash -c "echo \\\$PATH"`)

```

There is quite some effort to escape everything which is involved.  
Why?  
First, `run` doesn’t call a shell (e.g. bash) but executes the commend directly: [Running External Programs · The Julia Language](https://docs.julialang.org/en/v1.8/manual/running-external-programs/#Running-External-Programs)  
So, your attempt doesn’t run a bash, but (for me) `/bin/echo` :

```julia
julia> run(`bash -c "which echo"`)
/bin/echo
Process(`bash -c 'which echo'`, ProcessExited(0))

```

Second, just escaping single with `\$` is not enough. It just escapes Julias string interpolation. The bash, which is called sees `$PATH` and would again put the value of this variable, which results in something like:

```julia
echo /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin....

```

For me, this gives an error because I have some special characters in some paths. So, to avoid this, triple escaping:

```julia
\\\$PATH

```

which is equivalent to

```julia
\${PATH}

```

because you already avoided Julias string interpolation with `${PATH}`, so only a single escape is enough now.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [September 26, 2022, 4:20pm UTC](https://discourse.julialang.org/t/what-makes-bash-forget-the-environment-variable/87821/5 "2022-09-26T16:20:12Z")

</div>

> [@oheil](#):
>
> What you want to see or test is this:
> 
> `julia> run(`bash -c “echo \$PATH”`)`

Thanks, but this does not work. This does:

```julia-auto
run(`bash -c "echo \${PATH}"`) 

```

Edit: Wow, quoting code with backslashes really IS fiddly.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 26, 2022, 4:30pm UTC](https://discourse.julialang.org/t/what-makes-bash-forget-the-environment-variable/87821/6 "2022-09-26T16:30:21Z")

</div>

FYI: The ENV isn’t forgotten, you can also do at least this way (only bash interpolates with shell ENVs, and run doesn’t why you did what you did, but bash might not even be available, so this way might be preferable, and more portable):

```julia
P=ENV["PATH"]; run(`echo $P`)

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 26, 2022, 4:32pm UTC](https://discourse.julialang.org/t/what-makes-bash-forget-the-environment-variable/87821/7 "2022-09-26T16:32:49Z")

</div>

> [@PetrKryslUCSD](#):
>
> `run(`bash -c “echo ${PATH}”`) `

This seems to depend on the system. I tried out with WSL2 and this doesn’t work there. I had to use the triple escape version

```julia
run(`bash -c "echo \\\${PATH}"`)

```

here also.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [September 26, 2022, 4:35pm UTC](https://discourse.julialang.org/t/what-makes-bash-forget-the-environment-variable/87821/8 "2022-09-26T16:35:48Z")

</div>

> [@oheil](#):
>
> `run(`bash -c “echo \${PATH}”`)`

On windows, with julia running in a bash,

```julia-auto
julia> run(`bash -c "echo \\\${PATH}"`)                                                                  
${PATH}                                                                                                  
←[0mProcess(`←[4mbash←[24m ←[4m-c←[24m ←[4m'echo \${PATH}'←[24m`, ProcessExited(0))                      
                                                                                                         
julia> 

```

So, no, this didn’t work.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 26, 2022, 5:52pm UTC](https://discourse.julialang.org/t/what-makes-bash-forget-the-environment-variable/87821/9 "2022-09-26T17:52:46Z")

</div>

> [@oheil](#):
>
> > [@PetrKryslUCSD](#):
> >
> > `run(`bash -c “echo ${PATH}”`) `
> 
> This seems to depend on the system. I tried out with WSL2 and this doesn’t work there.

That didn’t even work on (my Mint) Linux. This works for me:

```julia-auto
julia> run(`bash -c "echo \${PATH}"`)

```

I’m confused why \\ would be needed in WSL2, or why you tried that way. It doesn’t get me the actual path, only

```julia-auto
julia> run(`bash -c "echo \\\${PATH}"`)
${PATH}
Process(`bash -c 'echo \${PATH}'`, ProcessExited(0))

```

I think the point of (bash in) WSL2 is that whatever you do works the same as in Linux, including the default (bash) shell you would run Julia under. I’m curious is anyone here running bash in Windows but not under WSL2? I mean is that possible? Then using Windows binary of Julia, not Linux binary of Julia.

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [September 26, 2022, 6:22pm UTC](https://discourse.julialang.org/t/what-makes-bash-forget-the-environment-variable/87821/10 "2022-09-26T18:22:16Z")

</div>

> [@Palli](#):
>
> `P=ENV["PATH"]; run(`echo $P`)`

You can interpolate the dict-key access directly too:

```julia
run(`echo $(ENV["PATH"])`)

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 26, 2022, 6:47pm UTC](https://discourse.julialang.org/t/what-makes-bash-forget-the-environment-variable/87821/11 "2022-09-26T18:47:04Z")

</div>

I just have to confirm your finding:

Native Debian bash:

```julia
julia> run(`bash -c "echo \\\${PATH}"`)
${PATH}
Process(`bash -c 'echo \${PATH}'`, ProcessExited(0))

julia> run(`bash -c "echo \${PATH}"`)
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Process(`bash -c 'echo ${PATH}'`, ProcessExited(0))

```

WSL2 (Debian) bash, running native Windows Julia from path  
/mnt/c/Users/Oli/AppData/Local/Programs/Julia-1.7.2/bin/julia.exe :

```julia
julia> run(`bash -c "echo \\\${PATH}"`)
/usr/local/sbin:/usr/local/bin:...
Process(`bash -c 'echo \${PATH}'`, ProcessExited(0))

julia> run(`bash -c "echo \${PATH}"`)
/bin/bash: -c: line 1: syntax error near unexpected token `('
/bin/bash: -c: line 1: `echo /usr/local/sbin:/usr/local/bin:...:/mnt/c/Program Files (x86)/Common Files/Oracle/Java/javapath: ...'
ERROR: failed process: Process(`bash -c 'echo ${PATH}'`, ProcessExited(2)) [2]

Stacktrace:
 [1] pipeline_error
   @ .\process.jl:531 [inlined]
 [2] run(::Cmd; wait::Bool)
   @ Base .\process.jl:446
 [3] run(::Cmd)
   @ Base .\process.jl:444
 [4] top-level scope
   @ REPL[2]:1

```

It’s always more complicated as it seems 😉

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [September 26, 2022, 8:51pm UTC](https://discourse.julialang.org/t/what-makes-bash-forget-the-environment-variable/87821/12 "2022-09-26T20:51:11Z")

</div>

What about WSL2 running Linux julia?

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [September 26, 2022, 9:15pm UTC](https://discourse.julialang.org/t/what-makes-bash-forget-the-environment-variable/87821/13 "2022-09-26T21:15:46Z")

</div>

What happens if you’re running windows inside a Linux VM which is a guest of a Windows bare metal 😂

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [September 26, 2022, 9:18pm UTC](https://discourse.julialang.org/t/what-makes-bash-forget-the-environment-variable/87821/14 "2022-09-26T21:18:06Z")

</div>

My question is about WSL2 running inside Windows 10, running inside Linux VM, hosted by Windows Server bare metal. Better?

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [September 26, 2022, 9:23pm UTC](https://discourse.julialang.org/t/what-makes-bash-forget-the-environment-variable/87821/15 "2022-09-26T21:23:07Z")

</div>

Hopefully there’s a fixed point attractor for iteration…

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [September 27, 2022, 1:00am UTC](https://discourse.julialang.org/t/what-makes-bash-forget-the-environment-variable/87821/16 "2022-09-27T01:00:19Z")

</div>

This sort of confusion is _ **precisely** _ the reason why using system-dependent shell commands is wrong and you really don’t want to do it for portability.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [September 27, 2022, 1:57am UTC](https://discourse.julialang.org/t/what-makes-bash-forget-the-environment-variable/87821/17 "2022-09-27T01:57:03Z")

</div>

OK, but when I know I have a Linux bash, taking advantage of all those powerful shell commands is a natural choice.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 27, 2022, 9:00am UTC](https://discourse.julialang.org/t/what-makes-bash-forget-the-environment-variable/87821/18 "2022-09-27T09:00:14Z")

</div>

Is there still something I should try out?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 27, 2022, 10:10am UTC](https://discourse.julialang.org/t/what-makes-bash-forget-the-environment-variable/87821/19 "2022-09-27T10:10:24Z")

</div>

> [@PetrKryslUCSD](#):
>
> What about WSL2 running Linux julia?

Downloaded Generic Linux on x86 inside WSL2 and checked:

```julia
julia> run(`bash -c "echo \\\${PATH}"`)
${PATH}
Process(`bash -c 'echo \${PATH}'`, ProcessExited(0))

julia> run(`bash -c "echo \${PATH}"`)
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:...
Process(`bash -c 'echo ${PATH}'`, ProcessExited(0))

```

Lesson learned:

**Don’t use native Windows Julia inside a WSL2 Linux!**

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [September 27, 2022, 2:30pm UTC](https://discourse.julialang.org/t/what-makes-bash-forget-the-environment-variable/87821/20 "2022-09-27T14:30:27Z")

</div>

> **[Shelling Out Sucks](https://julialang.org/blog/2012/03/shelling-out-sucks/)**
>
> Shelling Out Sucks | Spawning a pipeline of connected programs via an intermediate shell — a.k.a.

[Next page](https://discourse.julialang.org/t/what-makes-bash-forget-the-environment-variable/87821.md?page=2)
