Run a shell command from a child shell

I have a Unix shell test.sh script. Inside the script, I would like to call 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>

exit 0

What I intend to run shell-commands1 in shell-commandN from a child shell. Please tell me how to do this.

+3
source share
2 answers

You can set up a group, for example.

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

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

subshell

(i) creates a subshell in which you run a group of commands, otherwise a simple function will be executed. I do not know if POSIX is compatible (s).

Update. If I understand your comment correctly, you want to use the option -cwith bash, for example.

/bin/bash -c "Command1 && Command2...." &
+2
source

http://tldp.org/LDP/abs/html/subshells.html :

#!/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

Source: https://habr.com/ru/post/1784730/


All Articles