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

Bash script - redirect child script stderr to parent's stdout

I'm sure I'm missing something simple, but I'm using an executive script to call a few utility scripts and I want to handle all of the output from the utilities via one pipe. My issue is the utilities use stderr to report error conditions, but I…
Sam
  • 330
  • 1
  • 4
  • 11
5
votes
1 answer

Python subprocess, subshells, and redirection

I want to use the magic of subshells and redirection with the python subprocess module, but it doesn't seem to work, complaining about unexpected tokens are the parenthesis. For example, the command cat <(head tmp) when passed to subprocess gives…
pythonic metaphor
  • 10,296
  • 18
  • 68
  • 110
5
votes
1 answer

How to capture / redirect stdout/stderr from a source command into a variable (in a bash script)?

Usually I capture the output via a subshell: result="$(command 2>&1)" If the command is source, the subshell swallows some (all?) changes to the scripts shell's environment. How can I capture the output of source into a variable?
pico_prob
  • 1,105
  • 10
  • 14
5
votes
1 answer

Replacing 'source file' with its content, and expanding variables, in bash

In a script.sh, source a.sh source b.sh CMD1 CMD2 CMD3 how can I replace the source *.sh with their content (without executing the commands)? I would like to see what the bash interpreter executes after sourcing the files and expanding all…
5
votes
2 answers

Execute bash command with calling subshell $() via ssh with Here Document

I try to send command via ssh which looks like this: ssh user@192.168.1.1 "echo $(uname -a)" But my problem is, that $(uname -a) part actually create a subshell and executes not on 192.168.1.1 server, but on my system, from which I executed this…
ShockwaveNN
  • 2,227
  • 2
  • 29
  • 56
5
votes
1 answer

Parenthesis in bash - subshell vs grouping

In the manpage for bash, under the "Compound Commands" section, there are the following two entries: (list) list is executed in a subshell environment (see COMMAND EXECUTION ENVIRONMENT below). Variable assignments and builtin commands that…
Chris Middleton
  • 5,654
  • 5
  • 31
  • 68
5
votes
4 answers

bash: limiting subshells in a for loop with file list

I've been trying to get a for loop to run a bunch of commands sort of simultaneously and was attempting to do it via subshells. Ive managed to cobble together the script below to test and it seems to work ok. #!/bin/bash for i in {1..255}; do ( …
Nathaniel Saxe
  • 1,527
  • 2
  • 15
  • 25
5
votes
1 answer

Bash get process ID of a process started in subshell

I am looking at this question here, but it does not seem to be working for me: How to get the process id of a bash subprocess on command line So I have a command I start in a subshell, like: (sleep 10 &) How can I then get the process ID of that…
Jonovono
  • 1,979
  • 7
  • 30
  • 53
4
votes
1 answer

In POSIX sh, how to preserve variables set in a 'while read' loop when the input is continuous (event watcher)?

If you don't need to set global variables, or are willing to use named pipes, you can use: $NAMED_PIPE="/tmp/mypipe" mkfifo $NAMED_PIPE command | # piped into while read, command can be an event watcher while read -r foo bar; do …
lpf
  • 73
  • 1
  • 6
4
votes
2 answers

how to resolve warning ''using a subshell to avoid having to cd back'

The folder structure like this: folder_01 ├── folder_02├──Dockerfile_02 ├── folder_03├──Dockerfile_03 & example.sh In the example.sh script, I want to use cd to navigate back to folder_02 and use the dockerfile_02 to build an image and then…
wawawa
  • 2,835
  • 6
  • 44
  • 105
4
votes
2 answers

bash: `set -e` does not work when used in if-expression?

Have a look at this little script: #!/bin/bash function do_something() {( set -e mkdir "/opt/some_folder" # <== returns 1 -> abort? echo "mkdir returned $?" # <==…
frans
  • 8,868
  • 11
  • 58
  • 132
4
votes
1 answer

Bash command substitution + parameter expansion

I'm confused how the quoting and parameter and glob expansion is supposed to work in a subshell. Does the quoting and expansions of the subshell command line always happen in the context of the subshell process? My test seems to confirm…
Tuomas Toivonen
  • 21,690
  • 47
  • 129
  • 225
4
votes
1 answer

Does Powershell have an equivalent to the bash subshell?

One thing that's really great in linux bash shell is that you can define variables inside of a subshell and after that subshell completes the (environment?) variables defined within are just gone provided you define them without exporting them and…
leeand00
  • 25,510
  • 39
  • 140
  • 297
4
votes
2 answers

Why is stderr from a subshell not being suppressed or redirected?

I know that subshells have their stdout suppressed from the caller's output: a=$(echo 123) echo a:$a This outputs, as expected: a:123 But why isn't stderr suppressed as it's in a subshell? a=$(>&2 echo 123) Expected output: (nothing) Actual…
Zhro
  • 2,546
  • 2
  • 29
  • 39
4
votes
2 answers

Why does eval exit subshell mid-&& with set -e?

Why does bash do what I'd expect here with a compound command in a subshell: $ bash -x -c 'set -e; (false && true; echo hi); echo here' + set -e + false + echo hi hi + echo here here But NOT do what I'd expect here: $ bash -x -c 'set -e; (eval…
John Calcote
  • 793
  • 1
  • 8
  • 15
1 2
3
19 20