Questions tagged [subshell]

Use where a shell is run inside a shell (nested shells)

A sub-shell is a shell run inside another shell, typically the same shell. Sub-shell invocation typically uses some special syntax.

222 questions
156
votes
2 answers

Do parentheses really put the command in a subshell?

From what I've read, putting a command in parentheses should run it in a subshell, similar to running a script. If this is true, how does it see the variable x if x isn't exported? x=1 Running (echo $x) on the command line results in 1 Running…
Igorio
  • 7,279
  • 7
  • 20
  • 11
79
votes
3 answers

Is $() a subshell?

I understand the subshell syntax to be (), is $() just a subshell that you can retrieve variable values from? Note: This applies to bash 4.4 based on different wording in their documentation.
leeand00
  • 4,443
  • 10
  • 51
  • 78
70
votes
4 answers

Why does ( exit 1 ) not exit the script?

I have a script, that does not exit when I want it to. An example script with the same error is: #!/bin/bash function bla() { return 1 } bla || ( echo '1' ; exit 1 ) echo '2' I would assume to see the output: :~$ ./test.sh 1 :~$ But I…
Minix
  • 5,735
  • 8
  • 28
  • 45
46
votes
6 answers

exit shell script from a subshell

Consider this snippet: stop () { echo "${1}" 1>&2 exit 1 } func () { if false; then echo "foo" else stop "something went wrong" fi } Normally when func is called it will cause the script to terminate, which is…
Ernest A C
  • 739
  • 3
  • 7
  • 9
41
votes
3 answers

Bash subshell creation with curly braces

According with: 3.2.5.3 Grouping Commands {} { list; } Placing a list of commands between curly braces causes the list to be executed in the current shell context. No subshell is created. Using ps to see this in action This is the process…
iruvar
  • 16,515
  • 8
  • 49
  • 81
38
votes
2 answers

What happens when we type a simple command on shell?

I have a simple question regarding the execution of a simple command. As per my understanding, when we type a command such as ls in an interactive shell, Shell interprets the command. Shell creates a child process and executes the command on the…
MS.Kim
  • 1,667
  • 4
  • 18
  • 18
36
votes
3 answers

In Bash, how can I detect if I'm in a subshell?

I'm trying to write a function to replace the functionality of the exit builtin to prevent myself from exiting the terminal. I have attempted to use the SHLVL environment variable but it doesn't seem to change within subshells: $ echo $SHLVL 1 $ (…
jesse_b
  • 35,934
  • 12
  • 91
  • 140
36
votes
9 answers

Collect exit codes of parallel background processes (sub shells)

Say we have a bash script like so: echo "x" & echo "y" & echo "z" & ..... echo "Z" & wait is there a way to collect the exit codes of the sub shells / sub processes? Looking for way to do this and can't find anything. I need to run these subshells…
Alexander Mills
  • 9,330
  • 19
  • 95
  • 180
31
votes
3 answers

Rule for invoking subshell in Bash?

I seem to misunderstand the Bash rule for creating a subshell. I thought parentheses always creates a subshell, which runs as its own process. However, this doesn't seem to be the case. In Code Snippet A (below), the second sleep command does not…
bashful
  • 345
  • 1
  • 3
  • 5
26
votes
2 answers

Exporting a variable from inside a function equals to global export of that variable?

I use Ubuntu 16.04 with the native Bash on it. I'm not sure if executing #!/bin/bash myFunc() { export myVar="myVal" } myFunc equals in any sense, to just executing export myVar="myVal". Of course, a global variable should usually be declared…
Arcticooling
  • 1
  • 12
  • 44
  • 103
26
votes
3 answers

What is the exact difference between a "subshell" and a "child process"?

According to this and this, a subshell is started by using parenthesis (…). ( echo "Hello" ) According to this, this and this, a process is forked when the command is ended with a & echo "Hello" & The Posix specification use the word subshell in…
user232326
25
votes
3 answers

How does this script ensure that only one instance of itself is running?

On 19 Aug 2013, Randal L. Schwartz posted this shell script, which was intended to ensure, on Linux, "that only one instance of [the] script is running, without race conditions or having to clean up lock files": #!/bin/sh #…
user6860
23
votes
4 answers

How can I get the pid of a subshell?

How can I get the pid of a subshell? For example: $ echo $$ 16808 This doesn't work, because the original shell expands $$: $ ( echo $$ ) 16808 Why does single quoting not work? After the original shell removes the single quote, does the subshell…
Tim
  • 98,580
  • 191
  • 570
  • 977
23
votes
2 answers

Why is a variable visible in a subshell?

The Learning Bash Book mentions that a subshell will inherit only environment variables and file descriptors, etc., and that it will not inherit variables that are not exported: $ var=15 $ (echo $var) 15 $ ./file # this file include the same…
user3718463
  • 725
  • 6
  • 9
22
votes
4 answers

Is trap inherited by a subshell?

I tried a following script: #!/bin/bash trap 'echo "touching a file" && touch $FILE' EXIT foo1(){ echo "foo1" } foo(){ echo "foo" export FILE=${FILE:-/tmp/file1} } (foo1) foo The output for the above script was: [root@usr1…
1
2 3
14 15