WSL 看不到环境变量
WSL can't see environment variable
我目前正在寻求在 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 中使用 wsl <command>
执行 bash 命令时,输出不同。环境变量完全消失了。
C:\Users\Maxi>wsl ./test.sh
Staging dir is:
似乎.bashrc
(我也试过.profile
)在通过wsl执行时没有执行?`
这是一个问题,因为当 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 上 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
这是怎么回事?
~/.bashrc 文件仅由交互式 shell 加载。当您 运行 以这种方式编写脚本时,它由非交互式 shell 处理。您可以使用 -i
开关强制它进行交互(尽管我不知道这是否适用于 wsl
命令)。或者,在 运行 脚本之前将 BASH_ENV=/home/max/.bashrc
放入您的环境中。
我目前正在寻求在 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 中使用 wsl <command>
执行 bash 命令时,输出不同。环境变量完全消失了。
C:\Users\Maxi>wsl ./test.sh
Staging dir is:
似乎.bashrc
(我也试过.profile
)在通过wsl执行时没有执行?`
这是一个问题,因为当 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 上 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
这是怎么回事?
~/.bashrc 文件仅由交互式 shell 加载。当您 运行 以这种方式编写脚本时,它由非交互式 shell 处理。您可以使用 -i
开关强制它进行交互(尽管我不知道这是否适用于 wsl
命令)。或者,在 运行 脚本之前将 BASH_ENV=/home/max/.bashrc
放入您的环境中。