首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从子shell运行shell命令

从子shell运行shell命令
EN

Stack Overflow用户
提问于 2011-01-11 21:30:00
回答 2查看 4.1K关注 0票数 3

我有一个Unix shell脚本test.sh。在脚本中,我想调用另一个shell,然后从子shell执行shell脚本中的其余命令并退出

为了说明这一点:

test.sh

代码语言:javascript
复制
#! /bin/bash

/bin/bash /* create child shell */

<shell-command1>
<shell-command2>
......

<shell-commandN>

exit 0

我的目的是从子shell运行shell-command1到shell-commandN。请告诉我怎么做

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-01-11 21:36:06

你可以在一个组中设置,比如。

代码语言:javascript
复制
#!/bin/bash
(
Command1
Command2
etc..
)

subshell() {
    echo "this is also within a subshell"
}

subshell

(和)创建一个子subshell,您可以在其中运行一组命令,否则一个简单的函数就可以了。我不知道(和)是否兼容POSIX。

更新:如果我没理解错的话,你应该在bash中使用-c选项,比如。

代码语言:javascript
复制
/bin/bash -c "Command1 && Command2...." &
票数 2
EN

Stack Overflow用户

发布于 2011-01-11 21:36:04

下面是来自http://tldp.org/LDP/abs/html/subshells.html的一个示例:

代码语言:javascript
复制
#!/bin/bash
# subshell-test.sh

(
# Inside parentheses, and therefore a subshell . . .
while [ 1 ]   # Endless loop.
do
  echo "Subshell running . . ."
done
)

#  Script will run forever,
#+ or at least until terminated by a Ctl-C.

exit $?  # End of script (but will never get here).
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4658128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档