Questions tagged [subshell]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0
votes
0answers
47 views

Does ( ) really spawns a subshell? [duplicate]

I saw this video which explains that when running a command in parentheses it actually runs the command in a subshell, which is a child process of the original shell. Running the following experiment: ...
1
vote
0answers
23 views

How to capture the output of a Python program running in a subshell? [duplicate]

I'm trying to run a python program at startup by putting the following lines into etc/rc.local: (cd /home/pi/CODE/weather-project/weather-station && python main.py) &>> log.txt & ...
11
votes
1answer
2k views

Difference between subshells and process substitution

In bash, I want to assign my current working directory to a variable. Using a subshell, I can do this. var=$(pwd) echo $var /home/user.name If I use process substitution like so: var=<(pwd) ...
1
vote
0answers
24 views

Is the log-in shell the parent of shells in gnome terminal?

I have somehow thought that all shells that are invoked by a terminal emulator or any other way while a user is logged in are subshells of the log-in shell. Is this correct? If it matters, I am ...
0
votes
1answer
65 views

Launching a program via `xdg-open` from a subshell without blocking

I've noticed that calling xdg-open from a subshell will reliably block until the launched process is closed. I suspect there may be a reason for this, but I'm not sure as to why. For example, ...
3
votes
1answer
1k views

Setting bash flags in subshells - does it affect parent shell?

I have this in a bash script: ( set -o pipefail echo "foobar" | bash set +o pipefail ) do I need to reset pipefail setting, or can I just omit that line? In other words, does the pipefail ...
5
votes
3answers
341 views

Why must I put the command read into a subshell while using pipeline [duplicate]

The command df . can show us which device we are on. For example, me@ubuntu1804:~$ df . Filesystem 1K-blocks Used Available Use% Mounted on /dev/sdb1 61664044 8510340 49991644 15% /home ...
1
vote
1answer
52 views

How to continue running the script when a script called in the first script exited with an error code?

I am trying to write a bash script that will run a second script repeatedly until it fails and capture the stdout and stderr of that second script. I have managed to deal with most problems ...
0
votes
0answers
32 views

Why source same script twice is exiting the shell?

I have written a bash script to create a git worktree- cr_wt #!/bin/bash unset CDET_ID unset WT_DIR source ~/.bashrc usage() { echo "Usage: $0 [ -c CDET_ID ] [ -d Directory Name ]" ...
8
votes
3answers
5k views

Capture the output of a shell function without a subshell

I have rbenv (ruby version manager) installed on machine and it works like that: $ rbenv local 2.3.1 Writing to stdout the local version of my ruby. I want to rescue this version and declare it in a ...
0
votes
0answers
51 views

In bash, can I create an anonymous read-write pipe to a subshell?

I'm trying to pass a "resolution service" to a bash script as read-write file descriptor attached to another script. So given a "service" like capitalize: #!/usr/bin/env bash while ...
1
vote
2answers
57 views

Ordering output when two subshells write to stdout

I have a command of the below form input | tee >(subshell) | mainshell Both subshell and mainshell writes to stdout. So their output is not synchronised. E.g. echo "Hello\nWorld" | tee &...
1
vote
2answers
129 views

Isn't a subshell created when I run the `bash` command? [duplicate]

Isn't a subshell created when I run the bash command? For example, after executing bash, I cannot access the value of a non-exported variable. In this case, is the environment I switch to with the ...
1
vote
1answer
60 views

PID of a nested command in a subshell

Okay, I've got a program I'm trying to fix, for lengthy reasons a rewrite isn't the greatest option... So, I've been up for 8 hours trying to solve this and I'm completely stumped. I'm trying to get ...
1
vote
0answers
23 views

`set -e` doesn't abort the script when a sub-shell fails as part of `export` [duplicate]

When running the following script: #!/bin/bash set -e export X=$(false) echo That did not abort X=$(false) echo That did abort It outputs: That did not abort So export X= doesn't abort the execution,...
0
votes
2answers
53 views

Another called script not going into background even given &

I have 2 scripts. I'm calling my script2 as sh scriptpath/script2.sh & inside script1 which compresses files using a combination of find, xargs and gzip commands on 16 files at a time. It is ...
24
votes
8answers
27k views

Collect exit codes of parallel background processes (sub shells)

Say we have a bash script like so: echo "x" & echo "y" & echo "z" & ..... echo "Z" & wait is there a way to collect the exit codes of the sub shells / sub processes? Looking for way ...
1
vote
1answer
60 views

What is the difference between executing in subshells vs using & vs coproc command?

I was recently reading about subshells in linux. An example used was sleep 10. (sleep 10) - subshell sleep 10& - background process coproc sleep 10 It was written that the first command ...
4
votes
1answer
54 views

Can I determine if the current function has been invoked via command substitution?

foo() { if [[ something ]]; then echo "The foo function was called directly." else echo "The foo function was invoked via command substitution." fi } Call foo ...
0
votes
2answers
106 views

$! is empty when running command in bash with -c or in subshell

I am trying to get the pid of the previous command with $!, but that only works in the 'main' shell: $ echo "test" | echo $! 4436 $ If I start a subshell, then it's empty: $ bash $ echo &...
1
vote
2answers
133 views

less stops my script; why is that and how to avoid?

I have this Bash script named as s in current directory: #!/bin/bash pipe_test() { ( set -m; ( $1 ); set +m ) | ( $2 ) } pipe_test "$1" "$2" If I call e.g. ./s yes ...
1
vote
1answer
83 views

Differences between (command) and command in bash

While investigating the problem of redirecting the stderr of the bash keyword time and the standard solution to the problem, I came across another question. The description man bash gives of the (list)...
0
votes
1answer
90 views

Why does Bash run scripts in subshells?

Why does the Bash shell run scripts in subshells? What is the advantage of doing so?
0
votes
0answers
26 views

How a command which is meant to output indefinitely, when piped to another command that waits for a certain amount of input, knows when to terminate?

I am baffled by how piping in bash works. Take a random string generator, for example: tr -dc 'A-Z0-9' </dev/urandom spawned tr process that reads from /dev/urandom indefinitely, as far as /dev/...
1
vote
1answer
40 views

Different behavior of progress bars: local vs. remote ssh bash script

I am having issues to get minimal progress bars working in local scripts which are executed remotely via ssh. Depending on the command I get other undesired progress indicators (lines of dots with ...
9
votes
2answers
13k views

Putting subshell in background vs putting command in background

I have two bash scripts that try to check hosts that are up: Script 1: #!/bin/bash for ip in {1..254}; do ping -c 1 192.168.1.$ip | grep "bytes from" | cut -d" " -f 4 | cut -d ":" -f 1 & ...
0
votes
1answer
116 views

How to pass a subprocess to a command with bash

I'm trying to pass multiple outputs to a command using sub-processes. This is (a shortened version of) my command: cat "$1" one=cat "$1" The output of command <(echo "foo") is foo /dev/fd/63: ...
0
votes
1answer
142 views

How to exit the parent script as failed when the child script fails

I have a script which calls another script. Now even if the second script fails it's still succeeding original script. Below is the first part of the script: #OBJ_TYP=`echo $Job_detail_corr | awk -...
4
votes
1answer
201 views

Why is $! returning the wrong process ID for a subshell running in background?

$(sleep 5) & echo $! sleep 1 echo "done" The above outputs: $ ./test.sh 7483 done However if I search for sleep in ps aux I see: ps aux | rg sleep chris 7484 0.0 0.0 132788 1432 pts/6 ...
0
votes
2answers
162 views

Why grep --include with curly braces fail in subshell? [duplicate]

This works in shell: grep -R --exclude-dir=storage --exclude-dir=node-modules --include=*.{scss,css} loader.gif But this fails: sh -c "grep -R --exclude-dir=storage --exclude-dir=node-modules --...
1
vote
2answers
192 views

Does shell maths run in a subshell?

When I perform a simple math operation in #!/bin/sh, does that create a subshell? E.g., addition=$(( 1 + 1 )) The syntax would suggest a subshell, but I couldn't find anything on this
1
vote
2answers
540 views

Background process of subshell strange behaviour

I wonder why some similar bash commands behave the way they do. I have a bash script foo: #!/usr/bin/env bash while true do echo "reading" read data echo $data echo "stderr msg" >...
4
votes
1answer
297 views

In bash, how can I retrieve the exit code of a subshell when using local

This question is close to others - Can I get the exit code from a sub shell launched with $(command)? However there are no solutions I've found that allows me to get an exit code from a sub shell ...
4
votes
3answers
2k views

Silently kill subshell?

I want to implement something like this Q/A but for a sub-shell. Here is a minimal example of what I'm trying: (subshell=$BASHPID (kill $subshell & wait $subshell 2>/dev/null) & sleep ...
1
vote
1answer
216 views

Are subshells run in parallel by default?

This question follows from this one and one of its answers' recommendation to read Linuxtopia - Chapter 20. Subshells. I'm a bit confused by this statement at the Linuxtopia site: subshells let ...
0
votes
0answers
83 views

How can I get exit status of subshell?

A classical example of piping: $ false | true $ echo ${PIPESTATUS[@]} 1 0 Same example run in a subshell and assigning results into a variable: $ process="$(false | true)" $ echo ${PIPESTATUS[@]} 0 ...
19
votes
3answers
6k views

What is the exact difference between a “subshell” and a “child process”?

According to this and this, a subshell is started by using parenthesis (…). ( echo "Hello" ) According to this, this and this, a process is forked when the command is started with a & echo "...
10
votes
2answers
838 views

How to print and use a command output in a one-liner?

I'd like to pass the result of mktemp as argument to a command, let's say gcc -o. gcc -o $(mktemp) causes the result to be used, but I need to figure out the result. The only thing I could come up ...
3
votes
1answer
228 views

Nested command substitution does not stop a script on a failure even if -e and shopt -s inherit_errexit are set

Suppose you have a following script sandbox.sh. (This looks similar to Command substitution inside a function does not stop the script on a failure even if -e is set, but I believe a bit different ...
1
vote
0answers
42 views

How can I create a function that memoizes other commands?

I have created the following function to memoize any command: # zsh (( $+functions[ec] )) || { ec() print -r -- "$@" ecerr() { ec "$@" 1>&2 } ecdbg() ecerr "$@" gq() { ec "${(q+...
0
votes
1answer
73 views

Subshell Job Clarification

I run the command (Xorg & sleep 3; xeyes) & to test Xorg, and group it into a single subshell background job to make management easy. This works properly, and opens xeyes in the new Xorg ...
1
vote
1answer
179 views

Can you explain this inner working of this printf command in bash?

I stumbled onto the question Replacing only specific variables with envsubst and in one of the comments was this shell example: envsubst "$(printf '${%s} ' ${!PREFIX*})" < infile See the original ...
4
votes
0answers
144 views

Is job-control really meant to be supported in subshells and scripts?

Small premise I use subshells quite often to perform operations that involve changing the Shell Execution Environment, so as not to affect the main shell. I do it often from an interactive shell, and ...
5
votes
2answers
118 views

How to handle async subshell exit

Say I have this: set -e; ( docker stop notifier-server docker rm -f notifier-server exit 1 # explicitly exit with non-zero ) & wait; echo 'we are here now' will we always get to ...
0
votes
2answers
243 views

Set shell variable then invoke subprocess fails

I was playing with getopts, which has a shell variable OPTERR that influences its behavior. I wanted to change the value of OPTERR and invoke getopts on the same line, to affect the behavior of ...
1
vote
1answer
171 views

Export vars env to tmux's window/session/pane's env

Is there any way to export/upgrade list of variable env from a file or something to tmux's window/session/pane? Here I have to manually source .bashrc to update new var env to each tmux's pane.
0
votes
2answers
115 views

Precedence of subshells in relation to redirection?

Shell: GNU BASH I've been using the following command for a while now: echo "$(fmt -w 50 < foo.txt)" > foo.txt Without any issues. However, when I try: fmt -w 50 < foo.txt > foo.txt ...
11
votes
2answers
652 views

Why does a brace command group need spaces after the opening brace in POSIX Shell Grammar?

TL;DR: Why does POSIX brace group need spaces after { reserved word but subshell doesn't after reserved word (? POSIX shell grammar defines brace group and subshell as follows brace_group : ...
31
votes
3answers
6k views

How can I detect if I'm in a subshell?

I'm trying to write a function to replace the functionality of the exit builtin to prevent myself from exiting the terminal. I have attempted to use the SHLVL environment variable but it doesn't seem ...
15
votes
2answers
6k views

How can I get the pid of a subshell?

How can I get the pid of a subshell? For example: $ echo $$ 16808 This doesn't work, because the original shell expands $$: $ ( echo $$ ) 16808 Why does single quoting not work? After the ...