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
14
votes
5 answers

How to find next available file descriptor in Bash?

How can I figure out if a file descriptor is currently in use in Bash? For example, if I have a script that reads, writes, and closes fd 3, e.g. exec 3< <(some command here) ... cat <&3 exec 3>&- what's the best way to ensure I'm not interfering…
Kvass
  • 8,294
  • 12
  • 65
  • 108
13
votes
3 answers

Variables getting reset after the while read loop that reads from a pipeline

initiate () { read -p "Location(s) to look for .bsp files in? " loc find $loc -name "*.bsp" | while read do if [ -f "$loc.bz2" ] then continue else filcount=$[$filcount+1] bzip $loc fi if [ "$scan" == "1"…
user880248
13
votes
2 answers

What happens when reading into a variable in a pipeline?

echo hello | read str echo $str This read is executed after the pipeline, which means that the output of the echo gets read into str - but because it is after a pipe, the contents of str are now in a subshell that cannot be read by the parent…
capser
  • 2,442
  • 5
  • 42
  • 74
12
votes
1 answer

Subshell IO redirection

Given a file "foo.txt", created from: $ seq 1 10 > "foo.txt" I'm trying to read both the first and last line of the file, and I started by redirecting the file to a subshell, so that the list of commands will consume the file in sequence -- each…
Rubens
  • 14,478
  • 11
  • 63
  • 92
11
votes
1 answer

Bash 'swallowing' sub-shell children process when executing a single command

Bumped into an unexpected bash/sh behavior and I wonder someone can explain the rationale behind it, and provide a solution to the question below. In an interactive bash shell session, I execute: $ bash -c 'sleep 10 && echo' With ps on Linux it…
Marco
  • 588
  • 1
  • 4
  • 15
11
votes
2 answers

Why does `cat <(cat)` produce EIO?

I have a program that reads from two input files simultaneously. I'd like to have this program read from standard input. I thought I'd use something like this: $program1 <(cat) <($program2) but I've just discovered that cat…
i336_
  • 1,813
  • 1
  • 20
  • 41
11
votes
3 answers

Does a Shell function run in a sub-shell?

I'm trying to get around a problem that seems to me you cannot pass open db2 connection to a sub-shell. My code organization is as follows: Driver script (in my_driver.sh) # foo.sh defines baz() bar(), which use a db2 connection # Also the…
lzc
  • 919
  • 7
  • 16
10
votes
1 answer

Can a function be invoked in a bash subshell as background job?

Let's say I have a bash function Yadda() { # time-consuming processes that must take place sequentially # the result will be appended >> $OUTFILE # $OUTFILE is set by the main body of the script # No manipulation of variables in the main…
pepoluan
  • 6,132
  • 4
  • 46
  • 76
10
votes
2 answers

Local variables in bash: local vs subshell

As far as I know there are two ways to create local variables in a bash function: create a subshell or declare every variable as local. For example: # using local function foo { local count for count in $(seq 10) do echo $count …
Ignitor
  • 2,907
  • 33
  • 50
9
votes
1 answer

"basename" used in subshell returns "command not found"

When running this script: #!/bin/sh -ex if [[ $# -ne 1 ]]; then echo "./import-public-ssh-key.sh " exit 1; fi PATH=$1 KEY=$(basename ${PATH}) I get: ./import-public-ssh-key.sh: line 9: basename: command not…
mles
  • 4,534
  • 10
  • 54
  • 94
9
votes
1 answer

Difference between ( ) & and ( &)?

I'm wondering what's the difference between these two grammar in bash: ( &) and ( ) &. The only difference that I noticed is, (tty &) will return "not a tty" while (tty) & will return the current tty name, but why? To give an example, should I…
kawing-chiu
  • 2,675
  • 1
  • 14
  • 10
8
votes
3 answers

How to use `set -e` inside a bash command substitution?

I have a simple shell script with the following preamble: #!/usr/bin/env bash set -eu set -o pipefail I also have the following function: foo() { printf "Foo working... " echo "Failed!" false # point of interest #1 true # point of…
krispet krispet
  • 1,648
  • 1
  • 14
  • 25
8
votes
2 answers

Does trap work as expected while piping?

Here is minimal code for issue demonstration: http://pastebin.com/5TXDpSh5 #!/bin/bash set -e set -o pipefail function echoTraps() { echo "= on start:" trap -p trap -- 'echo func-EXIT' EXIT echo "= after set new:" trap -p #…
Maxim Kholyavkin
  • 4,463
  • 2
  • 37
  • 82
7
votes
0 answers

Assign and use of a variable in the same subshell

I was doing something very simple like: v=5 echo "$v" and expected it to print 5. However, it does not. The value that was just set is not available for the next command. I recently learnt that "In most shells, each command of a pipeline is executed…
fedorqui
  • 275,237
  • 103
  • 548
  • 598
7
votes
5 answers

bash: how to update an associative array in an implicit subshell?

The problem: I cannot update an array in a while loop. An illustration (not the actual problem): declare -A wordcounts wordcounts["sentinel"]=1000 ls *.txt | while read f; do # assume that that loop runs multiple times wordcounts[$f]=$(wc -w …
9000
  • 39,899
  • 9
  • 66
  • 104
1
2
3
19 20