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
3
votes
1 answer

Backgrounded subshells use incrementally more memory

I am starting 1000 subshells in the background in a loop. I assume they use roughly the same amount of memory. for i in `seq 1000`; do ( echo $i; sleep 100; )& done; However, they do not. Each new subshell eats up a little bit more…
andras
  • 6,339
  • 6
  • 26
  • 22
3
votes
2 answers

Bash run command in background inside subshell

I want to be able to bg a process inside a subshell as if it were not in a subshell. $( sleep 3 & ) just ignores the ampersand. I've tried: $( sleep 3 & ) $( sleep 3 & ) & $( sleep 3 ) & but nothing changes. Then I tried $( disown sleep 3 & ) which…
Nelson
  • 922
  • 1
  • 9
  • 23
3
votes
2 answers

Bash: Subshell behaviour of ls

I am wondering why I do not get se same output from: ls -1 -tF | head -n 1 and echo $(ls -1 -tF | head -n 1) I tried to get the last modified file, but using it inside a sub shell sometimes I get more than one file as result? Why that and how to…
Martin T.
  • 505
  • 5
  • 10
3
votes
2 answers

Using subshells or pipes in cmake commands?

I want to add a CMake target which, when made, will trigger the following: rm $(find "${CMAKE_SOURCE_DIR}" -name "*.rej" -or -name "*.orig") I tried this: add_custom_target(pclean COMMAND bash -c "rm $(find \"${CMAKE_SOURCE_DIR}\" -name…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
3
votes
1 answer

awk not working in bash -c "somecommand | awk '{print $2}'"

This command works as expected: (dpkg -l | egrep "linux.+3.8.0.*44" | awk '{print…
Mark Nichols
  • 1,407
  • 2
  • 19
  • 25
3
votes
2 answers

Bash subshell consumes stdin of the parent process

Let's say I have a main.sh script that will be calling one.sh via a subshell. one.sh: #! /bin/bash set -euo pipefail (if [ -t 0 ]; then echo "one little two little three little buses" else cat - fi) | awk '{ $1 = "111"; print $0…
Stephen Chu
  • 343
  • 2
  • 15
3
votes
2 answers

Does a bash subshell spawn a new `bash` process?

I am reading The TTY demystified. In the "Jobs and sessions" section there is an example of a user using an xterm: $ cat hello hello ^Z [1]+ Stopped cat $ ls | sort And there is a table listing the processes involved: xterm, bash…
fonini
  • 2,989
  • 3
  • 21
  • 39
3
votes
1 answer

Quotes Within a Variable Within a Sub-Shell

So here's a strange issue I've encountered; I have a script that's trying to build a set of commands to run via eval, and these need to run within a sub-shell as part of a bunch of other commands (so I can run them asynchronously). These commands…
Haravikk
  • 3,109
  • 1
  • 33
  • 46
3
votes
1 answer

How can I differentiate "command substitution" from "subshell" inside a script?

I need to differentiate two cases: ( …subshell… ) vs $( …command substitution… ) I already have the following function which differentiates between being run in either a command substitution or a subshell and being run directly in the…
Maxim Kholyavkin
  • 4,463
  • 2
  • 37
  • 82
3
votes
2 answers

How to pass argument to a subshell (by execute, NOT source) without command line argument?

For example, I have a main shell program main.sh, and I have another subshell program sub.sh. I want to pass a variable var to the subshell, but I do not want to use command-line argument. An example code is the following: #In…
return 0
  • 4,226
  • 6
  • 47
  • 72
3
votes
2 answers

Bash: Execute script in context of calling shell

My understanding is that when I execute a script inside a BASH shell using syntax like $ ./myscript.sh a sub-shell is started and the code runs in that shell, with STDOUT and STDERR output being printed on my shell, STDIN is taken from my shell.…
jwbensley
  • 10,534
  • 19
  • 75
  • 93
3
votes
1 answer

Why does tee wait for all subshells to finish?

I have a server script that runs mysqld and forks to continue running. As an example: ./mysqld & echo "Parent runs next line in script." Why does tee wait for the child process to end before it ends itself? EDIT: For…
neverendingqs
  • 4,006
  • 3
  • 29
  • 57
3
votes
4 answers

Bash: Export functions for use in xargs

When my bash scripts start getting complex, I usually break them up into functions. This applies especially to complex pipes, as a sequence of complicated pipe commands (e.g. containing while-loops) can quickly become hard to read. Even more so when…
kdb
  • 4,098
  • 26
  • 49
3
votes
2 answers

Limiting the number of subshells spawned

I'm trying to limit the amount of subshells that are spawned in a script that I'm using to sniff our internal network to audit Linux servers in our network. The script works as intended, but due to the way I'm nesting the for loop, it spawns 255…
drewrockshard
  • 2,043
  • 10
  • 35
  • 47
3
votes
1 answer

Global variable is reset if loop send output to pipe

According the bash(1) man pages, when I run the following: set -e x=2 echo Start $x while [ $((x--)) -gt 0 ]; do echo Loop $x; done | cat echo End $x The output will be: Start 2 Loop 1 Loop 0 End 2 After the loop (runs as a subshell) the variable…
Udi
  • 117
  • 8