0

How can I put the follow script to work properly?

1  #/bin/bash
2
3  # some commands
4
5  bash
6  # a lot of commands
7  # ...
8  exit
9  
10 bash
11 # A lot of other commands
12 # ...
13 exit
14 
15 exit 0

The problem is that when executing the script in shell, a new interactive bash shell is entered and the execution stopped right there waiting for standard input. As the sub-environment may not be BASH in practice, bash command in line 4 and 9 are just examples and that's why I need a new solution other than putting those commands into separate files and invoke.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Blane John
  • 65
  • 6
  • 1
    The question is not very clear - may be some actual code and results will help. You may however define these code snippets as functions and call the function in background - so the rest of the code will execute - check out https://bash.cyberciti.biz/guide/Putting_functions_in_background – Sharad Mar 29 '21 at 07:14
  • What do you actually want to do? It is very unclear to me why you would like to call for `bash` within a bash-script. – kvantour Mar 29 '21 at 08:48
  • Thanks Sharad but function probably not the way as it cannot provide with a new executing environment. Actually when using rootless container with buildah, "buildah unshare" must be used to enter the user namespace and then do some mount and chroot things. As bash/buildah unshare/chroot are all creating/entering new sub-environment and all of them has syntax like "bash/buildah unshare/chroot -- COMMAND" where COMMAND can be any legal command or valid script(files). If script file is not prefered, any other way to execute multi-commands one-time? – Blane John Mar 29 '21 at 10:07

1 Answers1

1

This is the documented behaviour. If you execute a mere bash, you get an interactive shell. If you want to run several bash commands in a child process - which probably is what you want, since you tagged your question subshell, you write

bash FILENAME

where FILENAME is the name of the file which contains the commands. If you want to run several bash commands inside the current process, you write one of

source FILENAME
. FILENAME

The differences between source and . are minor, and you find them described in the bash man page.

If you want to run several commands in a child process, without putting them into a separate file, you can do it with

(
   some command
   some other command
)
user1934428
  • 19,864
  • 7
  • 42
  • 87
  • Thanks, well I would name it as sub-environment which is more reasonable and executing bash in a bash script indeed create a subshell which is also kind of sub-environment. To be simpler (for sub-environment such as bash/buildah unshare/chroot etc.), I code the way my snippet appears above, but it fails to work as expected... – Blane John Mar 29 '21 at 10:20
  • 1
    The Unix / Linux term for this is a **process**. A process **is** not an environment, but it **holds** an environment. Of course you can create your own terminology, if you want, but you risk being misunderstood when discussing technical issues with your co-workers. – user1934428 Mar 29 '21 at 10:45
  • @BlaneJohn : See [here](https://www.tutorialspoint.com/unix/unix-processes.htm). I suggest that you familiarize yourself with the process model und Unix, because if you don't, you will get into trouble sooner or later when writing i.e. shell programs. – user1934428 Mar 30 '21 at 06:10