Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>

exit 0

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

like image 667
nitin_cherian Avatar asked Sep 11 '25 08:09

nitin_cherian


1 Answers

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...." &
like image 172
Anders Avatar answered Sep 13 '25 07:09

Anders