在新的 shell 中运行 bash 命令并在此命令执行后留在新的 shell 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7192575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
run bash command in new shell and stay in new shell after this command executes
提问by Wojciech Danilo
I've got a problem. I'm searching for long time for this answer - how can I run command in new bash shell and stay in this NEW shell after this commands executes. So for example:
我有问题。我正在寻找这个答案很长时间 - 如何在新的 bash shell 中运行命令并在执行此命令后留在这个新的 shell 中。例如:
bash -c "export PS1='> ' && ls"
will make new shell, export PS1, list directories and ... will exit to my current shell. I want to stay in the new one.
将创建新的外壳,导出 PS1,列出目录和...将退出到我当前的外壳。我想留在新的。
回答by Shawn Chin
You can achieve something similar by abusingthe --rcfileoption:
您可以通过滥用该--rcfile选项来实现类似的目标:
bash --rcfile <(echo "export PS1='> ' && ls")
From bash manpage:
--rcfile file
Execute commands from file instead of the system wide initialization file /etc/bash.bashrc and the standard personal initialization file ~/.bashrc if the shell is interactive
--rcfile 文件
如果 shell 是交互式的,则从文件而不是系统范围的初始化文件 /etc/bash.bashrc 和标准的个人初始化文件 ~/.bashrc 中执行命令
回答by dash-o
For the case where the initial set of command is static and contain multiple commands, it is usually easier to use here documentsto pass the initial commands, instead of constructing a script with series of echo commands.
对于初始命令集是静态的并且包含多个命令的情况,通常使用here documents传递初始命令更容易,而不是使用一系列 echo 命令构建脚本。
This approach helps when the commands contain quotes, or various expansions. With the quoted here-documents (the 3<<'__INIT__' ... '__INIT__') variant, no expansion of the here document text is performed, eliminating the need to quote specific part of the commands.
当命令包含引号或各种扩展时,此方法会有所帮助。使用引用的 here-document (the 3<<'__INIT__' ... '__INIT__') 变体,不执行 here 文档文本的扩展,从而无需引用命令的特定部分。
Instead of
代替
bash --rcfile <(echo "export PS1='> ' && ls && command1 && command2")
Use
用
bash --rcfile /dev/fd/3 3<<'__INIT__'
export PS1='> '
ls
command1
command2
__INIT__
回答by dimba
The lazy one:
懒人一:
bash -c "export PS1='> ' && ls; bash"

