Questions tagged [subshell]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
10 votes
1 answer
582 views

Different behaviour of subshells and && in bash vs zsh

My understanding of running commands in subshells is that the current shell is forked, and the subshell then does a further fork and exec on the desired commands. I see strange behaviour when running ...
user avatar
1 vote
0 answers
5 views

Queue interactive commands and attend to then in sequence

I want to have a script that adds commands to some kind of queue. I then want to run those commands interactively one at a time. More commands should be able to be added to the queue while I'm ...
user avatar
  • 11
0 votes
1 answer
34 views

How to pass more than one argument from `find -exec` to a `bash -c` sub-shell?

Is there a way to pass more than one argument from find -exec to a bash -c sub-shell? Given this working invocation which searches all videos matching a glob pattern (eg "*dogs.mp4) and then ...
user avatar
  • 1,425
7 votes
1 answer
478 views

Why wait in this script is not executed after all subshells?

In this script, that pulls all git repositories: #!/bin/bash find / -type d -name .git 2>/dev/null | while read gitFolder; do if [[ $gitFolder == *"/Temp/"* ]]; then ...
user avatar
0 votes
0 answers
29 views

How to know all subshells are executed and finished? [duplicate]

I have this script to pull all of the git repositories on my machine: #!/bin/bash find / -type d -name .git 2>&- | while read gitFolder; do if [[ $gitFolder == *"/Temp/"* ]]; ...
user avatar
0 votes
0 answers
31 views

How to clear the terminal in script while restoring entered commands after script is finished?

I'm currently writing a CLI file manager written in POSIX Shell. I want a cleared screen while running the script to not distract the user while browsing through the files. After the user quits the ...
user avatar
1 vote
1 answer
43 views

Subshell (subprocess/child process) with its own $BASHPID not listed by `ps`

I don't understand the following: mkfifo p; $ (>p ps -f | >>p echo $BASHPID) & [1] 983527 $ cat p 983529 PID PPID C STIME TTY TIME CMD 981815 165343 0 19:57 pts/27 00:...
user avatar
  • 234
0 votes
2 answers
145 views

Example of subshell without child-shell process

I just recently learned that "subshell" is not the same as "child shell process" (see for example What is the exact difference between a "subshell" and a "child ...
user avatar
  • 234
1 vote
1 answer
111 views

Setting a trap for INT doesn't work in a subshell

$ bash -c "trap \"echo INT\" INT; sleep 3" & pid=$!; sleep 1; kill -INT $pid; wait [1] 27811 INT [1]+ Done bash -c "trap \"echo INT\" INT; ...
user avatar
  • 2,893
0 votes
2 answers
141 views

bash: assign to variable using a subshell, but exit main script if assignment fails

I'd like to have a bash script that has allows me to call a function to see if some data is contained in a file, and if it isn't fail the main script, kind of like the following, which is simplified ...
user avatar
  • 1,327
1 vote
2 answers
149 views

Capture stdout of a crashing program and shell's "segfault error" message

I have a compiled program main.bin that is supposed to print: hello world another line This program generates a segmentation fault right after printing the first line. If I run it in the shell (zsh), ...
user avatar
0 votes
0 answers
20 views

Is there any difference between bash -c "command" vs. (command)? [duplicate]

bash -c runs a command in a subshell: bash -c "cd /tmp && ls" So does enclosing the command in parentheses: (cd /tmp && ls) Are these two techniques identical? If not, how ...
user avatar
  • 484
1 vote
1 answer
65 views

Killing `sleep` in a subshell

In bash, I'm observing that calling kill on a sleeping process won't kill it if I start out with a subshell. Can you help me understand why? function foo () { # Start a loop with `sleep` in the ...
user avatar
0 votes
1 answer
271 views

Parenthesis and curly brackets not working as expected in cron

I have set the following crontab for the root user: root@curie:/usr/home/username # crontab -l # /etc/crontab - root's crontab for FreeBSD # # $FreeBSD: release/10.0.0/en_US.ISO8859-1/books/handbook/...
user avatar
  • 11
0 votes
2 answers
120 views

Difference between $0 and $SHELL?

I am currently playing CTF in OverTheWire website . I was stuck on 32nd Level [ https://overthewire.org/wargames/bandit/bandit33.html ] to get password for next Level . Command to access Level 32 - ...
user avatar
1 vote
1 answer
89 views

zsh: Halt when command not found

I want to stop the zsh script running when it encounters a command not found error. I know of command_not_found_handler, but this function only allows me to print a message, not halt the parent shell. ...
user avatar
  • 1,287
0 votes
0 answers
19 views

Only execute subshell script if column 2 in Input.csv has value > 0

I have a long linux shell script (.sh) that has some R subshells. I only want to execute a subshell WEurope.sh if column 2 value in Input.csv has a value >0. Here is an example of Input.csv ...
user avatar
0 votes
2 answers
77 views

How to get caller function call method in bash

Based on the following code below, is it possible to obtain the value of $caller_method from the pseudocode function below whether a function's caller has made a function call normally eg: mytest 1 or ...
user avatar
  • 335
0 votes
1 answer
148 views

How can a subshell return data in a variable (not file) and output distinct logs at the same time?

Say I've got the following javascript: const something = doSomething(); function doSomething() { console.log("About to do something"); const a = ...
user avatar
0 votes
2 answers
1k 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 ...
user avatar
  • 479
1 vote
0 answers
44 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 ...
user avatar
  • 2,145
0 votes
0 answers
89 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 ...
user avatar
1 vote
2 answers
50 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 ...
user avatar
  • 233
0 votes
0 answers
335 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':...
user avatar
0 votes
1 answer
246 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 -...
user avatar
  • 778
0 votes
0 answers
101 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: ...
user avatar
  • 264
1 vote
0 answers
39 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 & ...
user avatar
  • 111
2 votes
0 answers
76 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 ...
user avatar
  • 153
0 votes
1 answer
553 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, ...
user avatar
  • 186
4 votes
3 answers
632 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 ...
user avatar
  • 2,809
1 vote
1 answer
844 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 ...
user avatar
  • 13
0 votes
0 answers
108 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 ]" ...
user avatar
  • 205
1 vote
0 answers
266 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 ...
user avatar
1 vote
2 answers
216 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 ...
user avatar
  • 1,072
1 vote
2 answers
132 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 &...
user avatar
  • 4,039
1 vote
1 answer
138 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 ...
user avatar
1 vote
0 answers
66 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,...
user avatar
0 votes
2 answers
58 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 ...
user avatar
1 vote
1 answer
201 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 ...
user avatar
4 votes
1 answer
65 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 ...
user avatar
0 votes
2 answers
462 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 &...
user avatar
1 vote
2 answers
236 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 ...
user avatar
  • 528
1 vote
1 answer
140 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)...
user avatar
0 votes
1 answer
365 views

Why does Bash run scripts in subshells?

Why does the Bash shell run scripts in subshells? What is the advantage of doing so?
user avatar
  • 1,431
0 votes
0 answers
46 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/...
user avatar
1 vote
1 answer
248 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 ...
user avatar
  • 13
0 votes
1 answer
357 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: ...
user avatar
0 votes
1 answer
437 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 -...
user avatar
  • 21
4 votes
1 answer
385 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 ...
user avatar
0 votes
2 answers
333 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 --...
user avatar
  • 1