WSL 看不到环境变量

WSL can't see environment variable

本文关键字:环境变量 看不到 WSL      更新时间:2023-10-16

我目前正在寻求在 WSL 中运行的 MIPS 交叉编译器工具链,由 CLion 访问。

在那里,我在 WSL 方面遇到了一个奇怪的问题。我在.bashrc中添加了以下内容:

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

此外,我有脚本test.sh

#!/bin/sh
echo "Staging dir is: " $STAGING_DIR

使用wsl获取 shell 并执行脚本时,输出如下:

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

使用wsl <command>在 WSL 中执行 bash 命令时,输出会有所不同。环境变量只是消失了。

C:UsersMaxi>wsl ./test.sh
Staging dir is:

似乎通过 wsl 执行时没有执行.bashrc(我也试过.profile(?

这是一个问题,因为当 CLion 调用 WSL 子系统以使用 CMake 生成我的C++项目时,编译器会抱怨此环境变量不存在。

当我手动获取 shell 并执行完全相同的 cmake 命令时,编译在没有警告的情况下工作,因为环境变量在那里。

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

在 WSL 外壳上:

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

这是怎么回事?

~/.bashrc文件仅由交互式shell加载。当您以这种方式运行脚本时,它由非交互式 shell 处理。您可以使用-i开关强制它是交互式的(尽管我不知道这是否适用于wsl命令(。或者,在运行脚本之前将BASH_ENV=/home/max/.bashrc放入环境中。