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
4
votes
2 answers

Why does shell command “{ command1; command2: } &" open a subshell?

As we all know, placing a list of commands between curly braces causes the list to be executed in the current shell context. No subshell is created. But when using "&" after "{}", why two subshells are created? pid 1002 and 1003. { ./a.out }…
Pengcheng
  • 439
  • 1
  • 4
  • 14
4
votes
1 answer

Is it possible to reuse readonly variables in bash sub-shells?

In Bash, variables declared inside sub-shells are distinct from variables with the same name declared in parent shells, unless the latter are explicitly exported: a=12 (a=13; echo $a) echo $a # Output: # 13 # 12 Outer variable wasn't…
swahnee
  • 2,661
  • 2
  • 24
  • 34
4
votes
3 answers

In bash how do I exit a script from a function that is piped by tee?

I'm trying to understand why whenever I'm using function 2>&1 | tee -a $LOG tee creates a subshell in function that can't be exited by simple exit 1 (and if I'm not using tee it works fine). Below the example: #!/bin/bash LOG=/root/log.log function…
LinenG
  • 109
  • 3
  • 14
4
votes
2 answers

Bash subshell for setting SHELLOPTS in a script

This question is not cygwin specific. However, in the cygwin mail archive https://cygwin.com/ml/cygwin-announce/2010-08/msg00015.html are various instructions for setting the cygwin specific igncr shellopt variable and one of them is the…
Maksim Grinman
  • 199
  • 1
  • 13
4
votes
1 answer

What happens if I source a script as background process?

From the BASH pages: When a script is run using source it runs within the existing shell, any variables created or modified by the script will remain available after the script completes. In contrast if the script is run just as filename,…
Pithikos
  • 18,827
  • 15
  • 113
  • 136
4
votes
2 answers

set -e ignored in subshell when followed by conditional control operator

I can't find any documentation indicating that this is correct behavior. I've verified this behavior on bash 4.2.25 and 4.1.2. Those are the latest to which I have simple access. Much appreciated if anybody has an explanation for why the…
Brian Chrisman
  • 3,482
  • 1
  • 15
  • 16
4
votes
1 answer

Run commands in subshell

I want to automate one repeated task which I do regularly. That is creating rpm's for different architectures. To compile the code and create the rpm I need to set the project env. after setting the env I will create the rpm for the current…
Dinesh Reddy
  • 775
  • 1
  • 11
  • 25
4
votes
1 answer

why $BASH_SUBSHELL doesn't increase in pipe

I know pipe will create subshell. I test $BASH_SUBSHELL and $BASHPID , and find $BASH_SUBSHELL doesn't increase in pipe echo $BASH_SUBSHELL # 0 echo $BASHPID # 8347 echo $BASH_SUBSHELL | cat # 0 echo $BASHPID | cat …
John Gauss
  • 41
  • 3
4
votes
2 answers

Trouble with errexit in bash

I'm writing a bash script and I'd like it to crash on the first error. However, I can't get it to do this in a specific circumstance I simplified below: #!/bin/bash set -Exu bad_command() { false #exit 1 echo "NO!!" } (set -o pipefail;…
Matei David
  • 2,322
  • 3
  • 23
  • 36
3
votes
3 answers

How to write a wrapper - avoiding a subshell - script for apache2 rewrite maps or shell scripts in general?

I am running a rewrite map with an external rewrite program (prg) in apache2 that may produce an error and die. When the rewrite map is not running any more the system obviously doesn't function properly. So I wanted to start a simple wrapper shell…
The Surrican
  • 29,118
  • 24
  • 122
  • 168
3
votes
1 answer

Running commands in a loop without spawning a new subshell each time

I have a bash script that reads in lots of dates in epoch time, and determines the (local) hour of the day at which they occurred. Relevant snippet: while read ts do hour="$(date -d@$((${ts} / 1000)) +%H)" ((hourly_counts["${hour}"]+=1)) done <…
chiastic-security
  • 20,430
  • 4
  • 39
  • 67
3
votes
2 answers

why does redirect (<) not create a subshell

I wrote the following code var=0 cat $file | while read line do var=$line done echo $var Now as I understand it the pipe (|) will cause a sub shell to be created an therefore the variable var on line 1 will have the same value on the last…
Aly
  • 15,865
  • 47
  • 119
  • 191
3
votes
1 answer

Trap signals both in script and subshells

I know there's a lot of questions similar to mine, but I didn't found any about trapping signals sent when a subshell is taking place on the terminal. Let me explain: #!/bin/sh trap 'echo "exiting.."; exit 0;' INT var1=$(echo "ab\nab" |…
Teodoro
  • 1,194
  • 8
  • 22
3
votes
1 answer

SC2207 Bash array assignment from subshell not splitting as expected

I have been populating an array using: AWS_STS_CREDS=( $(aws sts ...) ) This raises shellcheck error SC2207 Prefer mapfile or read -a to split command output But the recommendation does not work as expected. IFS=" " read -r -a AWS_STS_CREDS <<<…
beanaroo
  • 506
  • 1
  • 5
  • 14
3
votes
2 answers

Why does executing a simple command in a grouping command does not fork a subshell process, and the compound command will do it

I know that grouping commands(command-list) creates a subshell environment, and each listed command is executed in that subshell. But if I execute a simple command in the grouping command, (use the ps command to output the processes), then no…
Linke
  • 336
  • 1
  • 10