Questions tagged [subshell]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
-1
votes
0answers
26 views

Is there a POSIX rule for handling a trap in background process or in a subshell?

I have this shell script sub() { trap 'c=$?; trap - EXIT; echo EXIT $c; exit' EXIT trap 'c=$?; trap - EXIT; echo INT $c; exit' INT sleep 2 & wait $! } sub & pid=$! sleep 1 kill ...
0
votes
2answers
62 views

continue: only meaningful in a `for', `while', or `until' loop

I have a loop that checks for certain criteria for whether or not to skip to the next iteration (A). I realized that if I invoke a function (skip) that calls continue, it is as if it is called in a ...
1
vote
0answers
25 views

How to return a value from a job?

I want to spin up some jobs within a script to do some work in parallel. I will know when the work is done by counting the number of active jobs. The issue I'm having is that I want to also return a ...
0
votes
0answers
21 views

How to display banner text inside the subshell along with the results of whatever is being run in subshell

I'm trying to figure out how to display text inside a subshell along with the results of whatever is being run in the subshell, i.e: test=$(sudo echo foo) I've already set up a lecture_file so that a ...
0
votes
0answers
50 views

In what situations will combining coprocs with process lists be useful?

coproc will execute a command within a (named) subshell in the background mode. The naming feature is useful when running multiple coprocs within the same parent shell, and the background mode is ...
1
vote
2answers
35 views

Check if the script was already run in the current shell session

I have a script that's interactive if started without arguments, and I'd like it to print an ASCII-art logo the first time it's run in the current shell session. Is this possible to detect somehow? I ...
0
votes
0answers
58 views

Capturing output of sudo commands run over ssh (without password prompt)?

If I run this command, the output is what I expect, listing the directory /root on my server. ~ bash -c "ssh -t myserver 'sudo ls /root'" Enter passphrase for key '/home/james/.ssh/ed25519':...
0
votes
1answer
33 views

How are sudo permissions granted across parent/children processes?

I was having a look at how sudo grants work across parent/children processes, however, I'm confused. If I open a terminal (emulator, in a graphic environment), and execute: $ sudo bash -c "sudo -...
0
votes
0answers
55 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
27 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 & ...
1
vote
0answers
26 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
159 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, ...
5
votes
3answers
391 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
62 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
39 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
128 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
135 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
62 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
82 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
32 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
54 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
82 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
56 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
188 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
171 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
93 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
113 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
30 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
90 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
167 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
220 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
239 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
203 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
242 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
411 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 ...
1
vote
1answer
303 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
97 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 ...
10
votes
2answers
879 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
265 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
46 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
82 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
207 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
175 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
297 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
137 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
136 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
692 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 : ...
32
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 ...
0
votes
0answers
73 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
552 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: #!/...