Maximilian Gerhardt
Maximilian Gerhardt

Reputation: 5353

WSL can't see environment variable

I'm currently on the quest to getting a MIPS cross-compiler toolchain running within WSL, accessed by CLion.

There, I'm having a strange issue with WSL. I have added the following to the .bashrc:

STAGING_DIR="/home/max/source/staging_dir/toolchain-mipsel_24kc_gcc-5.4.0_musl-1.1.16"
export STAGING_DIR

Moreover I have the script test.sh

#!/bin/sh

echo "Staging dir is: " $STAGING_DIR

When using wsl to get a shell and execute the script, the output is as follows:

max@DESKTOP-ISH7UJQ:/mnt/c/Users/Maxi$ ./test.sh
Staging dir is:  /home/max/source/staging_dir/toolchain-mipsel_24kc_gcc-5.4.0_musl-1.1.16

When using wsl <command> to execute a bash command within WSL, the output differs. The environment variable has simply dissappeared.

C:\Users\Maxi>wsl ./test.sh
Staging dir is:

It seems like the .bashrc (I've also tried .profile) is not executed when executing it via wsl?`

This is a problem because when CLion invokes the WSL subsystem to build my C++ project using CMake, the compiler complains that this environment variable isn't there.

When I manually get a shell and execute the exact same cmake command, compilation works without warnings since the environment variable is there.

Inside CLion:

/usr/bin/cmake --build /mnt/c/Users/Maxi/CLionProjects/linux_wsl_test/cmake-build-debug-wsl_omega --target all -- -j 2
Scanning dependencies of target linux_wsl_test
[ 50%] Building CXX object CMakeFiles/linux_wsl_test.dir/main.cpp.o
mipsel-openwrt-linux-g++: warning: environment variable 'STAGING_DIR' not defined
[100%] Linking CXX executable linux_wsl_test
mipsel-openwrt-linux-g++: warning: environment variable 'STAGING_DIR' not defined
mipsel-openwrt-linux-g++: warning: environment variable 'STAGING_DIR' not defined
[100%] Built target linux_wsl_test

On a WSL shell:

max@DESKTOP-ISH7UJQ:~/build_dir$ /usr/bin/cmake --build /mnt/c/Users/Maxi/CLionProjects/linux_wsl_test/cmake-build-debug-wsl_omega --target all -- -j 2
[ 50%] Building CXX object CMakeFiles/linux_wsl_test.dir/main.cpp.o
[100%] Linking CXX executable linux_wsl_test
[100%] Built target linux_wsl_test

What's going on here?

Upvotes: 4

Views: 4554

Answers (1)

Kurtis Rader
Kurtis Rader

Reputation: 7459

The ~/.bashrc file is only loaded by interactive shells. When you run your script in that fashion it is handled by a non-interactive shell. You can force it to be interactive by using the -i switch (although I don't know if that works with the wsl command). Alternatively, put BASH_ENV=/home/max/.bashrc in your environment before running the script.

Upvotes: 5

Related Questions