Questions tagged [subshell]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
4 votes
2 answers
784 views

I need to have each line of a file run in a subshell of its own

I have a script that works fine, but runs in series. I would like to have the script take each line from a text file and run/spawn that line in a subshell and then move onto the next line and so on. I ...
0 votes
1 answer
45 views

How can I detect if I am in subshell, in bash and ZSH?

This question is different from In Bash, how can I detect if I'm in a subshell? because it's not shell specific. Let's say I open a terminal, which is zsh. I can go deep into that like this, $ ...
0 votes
2 answers
48 views

Passing a variable to subshell

Contrived example: #!/usr/bin/bash MYVAR=$(cat /somedir | grep -i myval) Now I want: #!/usr/bin/bash BASEDIR=/somedir MYVAR=$(cat [BASEDIR?] | grep -i myval) How should variable be passed to ...
  • 218
0 votes
1 answer
23 views

How does a trap affect external programs?

When a subshell is entered, traps that are not being ignored shall be set to the default actions Source: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_28 Apparently,...
  • 2,260
9 votes
2 answers
933 views

Can a script be run as a subshell?

Scripts typically start with a shebang such as #!/usr/bin/env bash, which specifies the shell to be used for execution. The execution behavior when the shebang is not present seems to be up to the ...
  • 2,133
0 votes
1 answer
174 views

How to curl/wget a script, run it with arguments, and make its functions available locally?

This function will either use cURL or wGet to download a script and execute it with additional arguments: wexec() { url=$1 shift if command -v "curl" >/dev/null 2>&1; ...
0 votes
1 answer
96 views

error handling in a bash subshell

I'm writing some bash code, and I'd like the script to exit if there's any error. The set -e trick works pretty well, but not with subshells. Here's a simplified example: set -e chmod a=r file.txt x=`...
  • 31
0 votes
1 answer
58 views

Bash: is it possible at all to trap SIGTERM (or other) in a subshell?

Consider multiple subshells, each writing its own log file. Consider that one subshell failed, leading to sending (using some mechanism) of SIGTERM to all the subshells. Upon reception of SIGTERM all ...
  • 457
1 vote
1 answer
44 views

Bash: how to check if any subshell failed?

Consider this code: job() { local id=$1 sleep $id } do_job_in_parallel() { local pids=() # run subshells for id in $(seq 4) do job $id & pids=("${pids[@]}" $!) ...
  • 457
0 votes
1 answer
148 views

Cannot Display Bash Functions within FZF Preview Window

How do I get the FZF Preview Window to Display Functions from my Current Bash Environment? I want to list my custom bash functions using FZF, and view the code of a selected function in the FZF ...
2 votes
1 answer
182 views

Capture output of a command that get stuck

I am trying to use regex for capturing some text pattern in an ouput of a command and create an array list_mqs. The problem is that this previous command get stuck and never terminates. So, when I use ...
  • 23
0 votes
0 answers
30 views

$(declare -p) keeps adding backslashes

From help declare: -p display the attributes and value of each NAME Just running declare -p works as expected. So does running it in a subshell with (declare -p). However, when using echo &...
0 votes
1 answer
29 views

Unable to get multicharacter field in fzf preview

_select () { apt-cache search '' | fzf \ --prompt='search: '\ --marker="M" \ --ansi \ --layout=reverse \ --cycle \ --multi \ --inline-info \ --preview "...
0 votes
1 answer
97 views

Why is /dev/null needed to run asynchronous jobs in busybox sh?

I'm curious why this special device is needed to fork the command and run it asynchronously in the minimal Busybox shell. BusyBox v1.30.1 (Debian 1:1.30.1-4) built-in shell (ash) Enter 'help' for a ...
0 votes
2 answers
48 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
47 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 ...
  • 55
0 votes
1 answer
225 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
63 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
69 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
120 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
884 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
19 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
192 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,565
7 votes
1 answer
552 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
33 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/"* ]]; ...
1 vote
1 answer
101 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:...
  • 260
0 votes
2 answers
376 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 ...
  • 260
1 vote
1 answer
417 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; ...
  • 3,093
0 votes
2 answers
565 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,569
2 votes
2 answers
556 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
354 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
770 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
481 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
290 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,413
0 votes
2 answers
311 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 ...
  • 375
0 votes
1 answer
616 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 ...
  • 561
1 vote
0 answers
151 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,345
0 votes
0 answers
206 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
2 answers
168 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 ...
  • 219
0 votes
0 answers
538 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
562 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 -...
  • 849
0 votes
0 answers
226 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: ...
  • 312
1 vote
0 answers
41 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
148 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
1k 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, ...
  • 266
5 votes
3 answers
1k 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 ...
  • 3,009
1 vote
1 answer
3k 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
259 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 ]" ...
  • 229
1 vote
0 answers
504 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
2 3 4 5