Questions tagged [subshell]

A subshell refers to a shell invoked from within a parent shell. It is an example of a child process. Questions with this tag may involve any issue with working with subshells. Please also include tags for platform or shell language.

Most questions with this tag involve the Bash shell, as that is a very popular shell language. However this could relate to any platform, command-line application, or shell that allows the creation of subshells.

295 questions
191
votes
8 answers

Why is $$ returning the same id as the parent process?

I have problem with Bash, and I don't know why. Under shell, I enter: echo $$ ## print 2433 (echo $$) ## also print 2433 (./getpid) ## print 2602 Where getpid is a C program to get current pid, like: int main() { printf("%d",…
ruanhao
  • 4,663
  • 6
  • 28
  • 43
107
votes
7 answers

How to effectively abort the execution of a Bash script from a function

I was using the "exit 1" statement in my Bash functions to terminate the whole script and it worked fine: function func() { echo "Goodbye" exit 1 } echo "Function call will abort" func echo "This will never be printed" But then I realized…
LiMar
  • 2,822
  • 3
  • 22
  • 28
79
votes
11 answers

Set a parent shell's variable from a subshell

How do I set a variable in the parent shell, from a subshell? a=3 (a=4) echo $a
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
78
votes
3 answers

How to pipe multiple commands into a single command in the shell? (sh, bash, ...)

How can I pipe the stdout of multiple commands to a single command? Example 1: combine and sort the output of all three echo commands: echo zzz; echo aaa; echo kkk desired output: aaa kkk zzz Example 2: rewrite the following so that all the…
Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
26
votes
2 answers

When does command substitution spawn more subshells than the same commands in isolation?

Yesterday it was suggested to me that using command substitution in bash causes an unnecessary subshell to be spawned. The advice was specific to this use case: # Extra subshell spawned foo=$(command; echo $?) # No extra subshell command foo=$? As…
Caleb
  • 5,084
  • 1
  • 46
  • 65
24
votes
6 answers

Bash bad substitution with subshell and substring

A contrived example... given FOO="/foo/bar/baz" this works (in bash) BAR=$(basename $FOO) # result is BAR="baz" BAZ=${BAR:0:1} # result is BAZ="b" this doesn't BAZ=${$(basename $FOO):0:1} # result is bad substitution My question is which…
Matt
  • 8,367
  • 4
  • 31
  • 61
24
votes
7 answers

Jenkins pipeline sh does not seem to respect pipe in shell command

I am using a Jenkinsfile in a pipeline on version 2.32.2. For various reasons I want to extract the version string from the pom. I was hoping I wouldn't have to add the maven help plugin and use evaluate. I quickly came up with a little sed…
sporkthrower
  • 806
  • 1
  • 7
  • 19
21
votes
4 answers

Difference between $() and () in Bash

When I type ls -l $(echo file) output from bracket (which is just simple echo'ing) is taken and passed to external ls -l command. It equals to simple ls -l file. When I type ls -l (echo file) we have error because one cannot nest () inside external…
run4gnu
  • 333
  • 1
  • 2
  • 7
20
votes
1 answer

When are bash variables exported to subshells and/or accessible by scripts?

I'm confused over whether bash variables are exported to subshells and when they are accessible by scripts. My experience so far led me to believe that bash variables are automatically available to subshells. E.g.: > FOO=bar > echo $FOO bar > (echo…
StoneThrow
  • 5,314
  • 4
  • 44
  • 86
19
votes
4 answers

Why avoid subshells?

I've seen a lot of answers and comments on Stack Overflow that mention doing something to avoid a subshell. In some cases, a functional reason for this is given (most often, the potential need to read a variable outside the subshell that was…
ruakh
  • 175,680
  • 26
  • 273
  • 307
18
votes
4 answers

How do I activate a conda env in a subshell?

I've written a python program. And if I have a shebang like this one: #!/usr/bin/python and I make the file executable with: $ chmod 755 program.py I can run the program like so: $ ./program.py Here is the issue. I use the conda virtual…
meh
  • 2,591
  • 4
  • 20
  • 33
17
votes
4 answers

Get exit code from subshell through the pipes

How can I get exit code of wget from the subshell process? So, main problem is that $? is equal 0. Where can $?=8 be founded? $> OUT=$( wget -q "http://budueba.com/net" | tee -a "file.txt" ); echo "$?" 0 It works without tee, actually. $> OUT=$(…
16
votes
3 answers

Why does ssh wait for my subshells without -t, and kill them with -t?

I have a bash script start.sh which looks like this: for thing in foo bar; do { background_processor $thing cleanup_on_exit $thing } & done This does what I want: I run start.sh, it exits with code 0, and the two subshells…
gubbins
  • 581
  • 4
  • 12
14
votes
2 answers

Left side of pipe is the subshell?

Edit: My comment below regarding sed 's@^@ @' <(f1) is incorrect While $BASH_SUBSHELL indicates that we are in the same level as the launch, the variables are lost in the main script. based on Gordons answer I tested f1 > >(sed 's@^@ @')…
nhed
  • 5,774
  • 3
  • 30
  • 44
1
2 3
19 20