开发者

Run shell command from child shell

I have a Unix shell script test.sh. Within the script i would like to invoke another shell and then execute the rest of the commands in the shell script from the child shell and exit

To make it clear:

test.sh

#! /bin/bash

/bin/bash /* create child shell */

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

<shell-commandN>

ex开发者_运维百科it 0

What my intention is to run the shell-commands1 to shell-commandN from the child shell. Kindly tell me how to do this


You can setup in a group, like.

#!/bin/bash
(
Command1
Command2
etc..
)

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

subshell

( and ) creates a subshell in which you run a group of commands, otherwise a simple function will do. I don't know if ( and ) is POSIX compatible.

Update: If I understand your comment correctly, you want to be using -c option with bash, like.

/bin/bash -c "Command1 && Command2...." &


From http://tldp.org/LDP/abs/html/subshells.html here is an example:

#!/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).
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜