如何使用预加载的“.bash_aliases"执行 WSL 命令;来自 PowerShell [英] How to execute WSL command with preloaded ".bash_aliases" from powershell
问题描述
我需要从 CMD/PS 运行 WSL 命令,而不实际进入 WSL shell,例如:wsl -d myDistro -u myUser echo $MY_EXP_VAR
我在 ~/.bash_aliases
中定义了我的变量和别名(它们也在 .profile
、.bashrc
、.bash_profile
,当我打开 WSL shell 时,只需通过 wsl [enter]
或使用这些参数,所有别名和导出的变量都可供我使用,但是当我尝试运行 as show 时在 wsl -d myDistro -u myUser echo $MY_EXP_VAR
它打印空行之前,当我运行 wsl -d myDistro -u myUser cat ~/.bash_aliases
它正确打印文件.我已经尝试添加
I need to run WSL commands from CMD/PS, without actually entering the the WSL shell, eg:
wsl -d myDistro -u myUser echo $MY_EXP_VAR
I have my variables and aliases defined in ~/.bash_aliases
(they were also in .profile
, .bashrc
, .bash_profile
, and when I open the WSL shell, just by wsl [enter]
or with those params, all the aliases and exported vars are available for me, but when I try to run the as show before wsl -d myDistro -u myUser echo $MY_EXP_VAR
it prints empty line, when I run wsl -d myDistro -u myUser cat ~/.bash_aliases
it prints the file correctly.
I already tried to add
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
在 /etc/profile
或 /etc/bash.bashrc
的末尾,但这没有帮助.任何提示如何添加我的变量以便我可以在不启动 shell 的情况下执行命令?我需要它来从 AndroidStudio 运行命令作为运行配置,而不是从终端运行.
at the end of /etc/profile
or /etc/bash.bashrc
but this didn't help.
Any hints how to add my vars so I can execute commands without starting the shell? I need this to run commands from AndroidStudio as a run config, not from terminal.
推荐答案
目前在移动设备上,所以我没有机会仔细检查这个,但从记忆中......
On mobile at the moment, so I haven't had a chance to double check this, but from memory ...
试试:
wsl -d myDistro -u myUser -e bash -li -c 'echo $MY_EXP_VAR'
这应该允许您的配置文件执行,因为您将 bash 作为登录 shell 运行并要求它也提供交互式启动脚本.如果您不强制使用 -li
,WSL 会以非交互方式并作为非登录 shell 运行 Bash,这解释了为什么您的配置无法获取源代码.
That should allow your profile to execute since you are running bash as a login shell and asking it to source the interactive startup scripts as well. If you don't force the -li
, WSL runs Bash non-interactively and as a non-login shell, which explains why your config doesn't get sourced.
这篇关于如何使用预加载的“.bash_aliases"执行 WSL 命令;来自 PowerShell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!