Questions tagged [subshell]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0 votes
2 answers
21 views

bash "catch block" is not catching bad subshell

So I have this: ( set -eo pipefail; { set -eo pipefail; file_path="$(echo "$i" | jq -r '.file_path')" if [[ -n "$file_path" ...
0 votes
2 answers
41 views

Bash use arguments returned from subshell in current shell

Sorry if the title is bad (it is already the best one I can think of). Anyway, here is the line of code: printf "%s\n" "$(echo a b "c d") Simple code. Of course, I will ...
  • 25
0 votes
1 answer
52 views

Bash, grep - why is a command with a caret not working in a subshell?

I'm trying to determine if a git add action has created any expected modifications, and the following works in an interactive shell $ BASE_DIR=foo/bar/baz $ git add "${BASE_DIR}" $ git ...
  • 229
1 vote
1 answer
30 views

Why do I need to disable=SC2031

Can you tell me, why I have to # shellcheck disable=SC2030 in the following script? get_names_and_hosts(){ unset LOCAL_HOSTS declare -a LOCAL_HOSTS unset LOCAL_NAMES declare -a LOCAL_NAMES ...
  • 143
0 votes
2 answers
29 views

How to design reusable script handling temporary files lifecycle for other scripts

I need to create a reusable ("utility") script handling entire set of operations with temporary files for any of my other ("application") scripts: creation tracking of temporaries ...
1 vote
4 answers
80 views

How to use a value from sub shell in parent shell

I'm not used to linux scripting and this is the first time I'm working on it so I'm struggling with the following problem: Code: while [ $pct -gt 80 ]; do flag=1; ls -tr | while read file; do ...
10 votes
1 answer
702 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 ...
1 vote
0 answers
13 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 ...
  • 11
0 votes
1 answer
71 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 ...
  • 1,495
7 votes
1 answer
501 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 ...
0 votes
0 answers
31 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/"* ]]; ...
0 votes
0 answers
140 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 ...
1 vote
1 answer
68 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:...
  • 244
0 votes
2 answers
227 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 ...
  • 244
1 vote
1 answer
227 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; ...
  • 2,963
0 votes
2 answers
267 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 ...
  • 1,437
1 vote
2 answers
310 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), ...
1 vote
1 answer
181 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 ...
0 votes
1 answer
491 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/...
  • 11
0 votes
2 answers
257 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 - ...
1 vote
1 answer
166 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. ...
  • 1,335
0 votes
2 answers
175 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 ...
  • 335
0 votes
1 answer
270 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 = ...
1 vote
2 answers
2k 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 ...
  • 501
1 vote
0 answers
77 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 ...
  • 2,235
0 votes
0 answers
125 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 ...
1 vote
2 answers
70 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 ...
  • 229
0 votes
0 answers
471 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
1 answer
387 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 -...
  • 809
0 votes
0 answers
152 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: ...
  • 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 & ...
  • 111
2 votes
0 answers
113 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 ...
  • 153
0 votes
1 answer
839 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, ...
  • 196
5 votes
3 answers
840 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 ...
  • 2,891
1 vote
1 answer
2k 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 ...
  • 13
0 votes
0 answers
183 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 ]" ...
  • 215
1 vote
0 answers
357 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
2 answers
254 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,132
1 vote
2 answers
151 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 &...
  • 4,167
1 vote
1 answer
198 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
0 answers
82 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
2 answers
61 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
1 answer
248 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
1 answer
83 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
2 answers
543 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 &...
2 votes
2 answers
264 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 ...
  • 558
1 vote
1 answer
154 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
1 answer
496 views

Why does Bash run scripts in subshells?

Why does the Bash shell run scripts in subshells? What is the advantage of doing so?
  • 1,461
0 votes
0 answers
55 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
1 answer
338 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 ...
  • 13

1
2 3 4 5