Questions tagged [subshell]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1
vote
1answer
35 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
27 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 ]" ...
0
votes
0answers
30 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
127 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
2answers
49 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
1answer
53 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
21 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
52 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 ...
1
vote
1answer
50 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
53 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
72 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
126 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
82 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
81 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
30 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 ...
0
votes
1answer
100 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
113 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
184 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
142 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
174 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
4
votes
1answer
270 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 ...
0
votes
0answers
204 views

Bash completion not working in subshell

If I open a terminal window, or open a terminal in tmux, tab completion works as expected, but if I type bash to open a subshell, it doesn't. Below is my .bash_profile and .bashrc # .bash_profile [[ ...
1
vote
1answer
185 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
72 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 ...
0
votes
0answers
80 views

Terminate a subshell background process

I am starting entr as a subshelled background process as described in its man page to listen to changes to some file foo, occuring during interactive use of some program baz, and I would like to ...
10
votes
2answers
820 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
209 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
67 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
162 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
135 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 ...
0
votes
2answers
222 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 ...
5
votes
2answers
115 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
106 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
626 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 : ...
30
votes
3answers
5k 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 ...
0
votes
0answers
67 views

Run script in background using `&` or `()&`?

If I want to run a script in background, for example a.sh, should I run ./a.sh & or should I put it in a subshell then background it? (./a.sh) & I test both and don't see difference, both ...
2
votes
1answer
364 views

Bash script to count number of executions instances does not work

I am writing a script that detects if there is any instance of it already running on my Linux machine and shows on screen the number of instances. The content of the "detect_itself.sh" script is: #!/...
1
vote
1answer
71 views

Is it possible for sendmail to not use all the output from a bash subshell piped to it?

( GNU bash, version 4.2.50(1)-release (powerpc-ibm-aix6.1.2.0)) ( echo "a" echo "b" echo "c" )| sendmail -F "Sender" recipients@a.com This runs inside a .sh file, and the log (when ran with the -x ...
-2
votes
2answers
47 views

Identifying shell script's subshell number from within a script itself

I am accidentally caught in a desire to reveal the subshell number (BASH_SUBSHELL) from within the script itself, but i get subshell 0 Here is the script's line echo "Operated from subshell: $...
0
votes
1answer
112 views

Exported Date Variable not appearing in subshell

I'm currently working on creating 2 bashes with two different scripts which result in two files named with the same timestamp for easy comparison/processing later. The date variable gets created in ...
1
vote
1answer
235 views

Terminating infinite-loop script embedded in tmux status bar

I have an infinite while loop bash script embedded in tmux status bar. In ~/.tmux/conf: set -g status-right '... #(path/loop.sh) ...' Tmux seems to run this script with sh -c. After tmux started: $ ...
4
votes
1answer
1k views

Process substitution inside a subshell to set a variable

I'm trying to run a script remotely and use its standard output to populate a variable. I'm doing this to avoid temporary files. Here's the pattern I'm trying: var=$(bash <(curl -fsSkL http://...
23
votes
3answers
3k views

How does this script ensure that only one instance of itself is running?

On 19 Aug 2013, Randal L. Schwartz posted this shell script, which was intended to ensure, on Linux, "that only one instance of [the] script is running, without race conditions or having to clean up ...
0
votes
1answer
198 views

How can I stop a child process of a subshell (as per SIGSTOP) before the subshell exits?

In bash, when running ( sleep 123 &), the sleep 123 process will continue running, when the subshell exits. How can I stop the sleep 123 process before its parent subshell exits? I'm trying to ...
0
votes
1answer
78 views

Confused why script does not exit

I have this script verbatim: #!/usr/bin/env bash handle_json(){ while read line; do cat <<EOF {"@json-stdio":true,"value":{"mark":"$1","v":"$line"}} EOF done; } ( echo; echo; echo '...
13
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 ...
0
votes
2answers
100 views

“Globally Store” PID of file started from bash script

I'm compiling a latex file in a script and then I'm trying to either open a new pdf reader window or reload the current one (llpp being my reader), so after compiling my script does pdflatex $...
8
votes
1answer
1k views

exit not terminating the script [duplicate]

exit doesn't terminate the script when error is called.. output Error: Could not resolve localhost after exit script #!/bin/sh resolve_ip (){ if [ -z "$1" ]; then host="localhost" ...