Questions tagged [subshell]

Use where a shell is run inside a shell (nested shells)

Filter by
Sorted by
Tagged with
2 votes
1 answer
88 views

Shell detection of empty subshell

SC1143 suggests that commented parts of a wrapped shell command to be wrapped in a subshell. Is the Posix shell "smart enough" to not launch a subshell when it sees that it does nothing? ...
AvidSeeker's user avatar
1 vote
1 answer
28 views

Obtaining the delta for 'declare -F' between current and clean shell environments

Main question: how would one get the delta for declare -F, between that in the current shell, and that as if the shell just started (first two commands below). $(declare -F) does not solve the ...
Erwann's user avatar
  • 677
4 votes
1 answer
369 views

Can a subshell create a subprocess?

So if I input the command like below, a sub-process (PID 4920) is created by the subshell. linuxprobe@DESKTOP-TP0G72N:~$ (sleep 2;ps -f --forest;sleep 5) UID PID PPID C STIME TTY ...
joe's user avatar
  • 53
1 vote
2 answers
84 views

Does the shell creating a subshell require the () Groups command?

In my book (Sobell's A Practical Guide to Linux, 4e) it is written that You can use the parentheses control operator to group commands. When you use this technique, the shell creates a copy of itself,...
EE18's user avatar
  • 233
0 votes
2 answers
105 views

What happens when I give the shell a "command" versus a script?

A variant of this question has been asked a few times (here and here, for instance) but I'm afraid that the answers either didn't fully capture my question here or else perhaps assumed more than I ...
EE18's user avatar
  • 233
0 votes
0 answers
41 views

Subshell and signal propagation

I am curious about signal propagation in subshells. I have the following bash script: #!/bin/bash function test_trap() { echo "hello" } trap 'test_trap' SIGUSR2 (#subshell trap '...
Daniel's user avatar
  • 1
0 votes
1 answer
49 views

How to indicate that the shell was spawned by ncdu - NCurses Disk Usage?

I employ ncdu, the NCurses Disk Usage tool, in conjunction with zsh and p10k. Pressing 'b' triggers the spawning of a shell in the current directory. How can I present this information on-screen to ...
alecail's user avatar
  • 1,623
2 votes
1 answer
58 views

Flock and bash strange chicken and egg problem

In which order are the distinct steps of this bash command done: (flock -n 9) 9> toto.txt If I do only the subshell part: (flock -n 9) I get this result: flock: 9: Mauvais descripteur de fichier (...
Laurent Lyaudet's user avatar
0 votes
0 answers
18 views

How can I signal/force the parent shell to change directory? [duplicate]

I have this command called reclone, and this is my script: . load sudo -v extract sudo rm -rf /$organization/$repository cd /$organization git clone --depth 1 [email protected]:$organization/$...
Saeed Neamati's user avatar
0 votes
1 answer
52 views

How to execute a subshell directly

I have this: timeout 25 bash -c ' for i in {1..9}; do if read line < "$my_fifo"; then if test "$line" != "0"; then exit 0; ...
Alexander Mills's user avatar
1 vote
1 answer
301 views

Why is stdin input replacing or corrupting my variables when running a script using SSH?

GNU bash, version 4.3.27 I'm running into an odd issue with a script that writes output to a file. The script ssh's to a list of servers and records the state/substate of a few services. I then store ...
Juts's user avatar
  • 13
0 votes
2 answers
77 views

how to make a subshell a "program"

I want to ignore all pre-existing env variables, and echo just one, like this: env -i <( export foo=bar; env ) and this would just print: foo=bar however, the construct of env -i <() ...
Alexander Mills's user avatar
0 votes
1 answer
183 views

Running a function as process with a set process name or id

I have a bash script set up to monitor a number of UDP streams and convert it into actionable data. My problem is that I need to set the script to periodically check to see if the stream capture is ...
Dan D's user avatar
  • 3
-1 votes
1 answer
50 views

Portable temporary file descriptors inside `sh -c` [duplicate]

cat <(echo yes) Displays "yes". And running this inside sh -m results in the same thing on Bash 5.2.15. Yet on Bash 4.4.20 it throws an error: sh -c "cat <(echo yes)" sh: -c:...
Zaz's user avatar
  • 2,599
-1 votes
2 answers
117 views

Avoiding subshell in conditional assignment

I'm a beginner at Linux shell, but I know that this command creates a subshell: Subshells are typically created using subshell operators or commands such as parentheses (), backticks `, or the $() ...
gremo's user avatar
  • 167
1 vote
1 answer
83 views

how do I read multiple variables from a sub shell

I've found similar problems on stackexchange, but never close enough to my case to help. I'm trying to create a script and bind it to a key, so that when pressed, it displays "title by artist&...
Dr. Coomer's user avatar
0 votes
2 answers
722 views

Get exit status of the first command in a pipe in the second the command

My question is similar to Get exit status of process that's piped to another, but I want to get the exit status of the second command in the pipe within the pipe. Here's an example - false | echo $...
MilkyDeveloper's user avatar
3 votes
1 answer
280 views

Unexpected difference between executing code in the shell or in a sub-shell

Is this difference between executing code in the shell or in a sub-shell expected? $ a() { echo ${@: -1} ; } $ echo "echo ${*: -1}" > b $ chmod +x b $ a 1 2 3 3 $ ./b 1 2 3 bash $ ...
gboffi's user avatar
  • 1,394
4 votes
2 answers
969 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 ...
Steve Rowe's user avatar
0 votes
1 answer
329 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, $ ...
Evan Carroll's user avatar
  • 30.9k
0 votes
2 answers
882 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 ...
paulj's user avatar
  • 238
0 votes
1 answer
109 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,...
finefoot's user avatar
  • 3,080
10 votes
2 answers
1k 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 ...
Jonathan H's user avatar
  • 2,393
0 votes
1 answer
861 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; ...
Martin Braun's user avatar
1 vote
1 answer
1k 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=`...
jyoung's user avatar
  • 91
0 votes
1 answer
276 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 ...
pmor's user avatar
  • 609
1 vote
1 answer
120 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[@]}" $!) ...
pmor's user avatar
  • 609
0 votes
2 answers
613 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 ...
user2514157's user avatar
2 votes
1 answer
470 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 ...
dcubaz's user avatar
  • 23
0 votes
0 answers
35 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 &...
rewire's user avatar
  • 101
0 votes
1 answer
37 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 "...
testoflow's user avatar
  • 117
0 votes
1 answer
232 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 ...
round-down's user avatar
0 votes
2 answers
256 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" ...
Alexander Mills's user avatar
0 votes
2 answers
81 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 ...
sudoer's user avatar
  • 45
0 votes
1 answer
430 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 ...
Bruno Kim's user avatar
  • 229
1 vote
1 answer
140 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 ...
Chris G.'s user avatar
  • 153
0 votes
2 answers
181 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 ...
wass rubleff's user avatar
1 vote
4 answers
245 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 ...
Biswadeep Sarkar's user avatar
10 votes
1 answer
2k 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 ...
pepe_silvia's user avatar
1 vote
0 answers
43 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 ...
AlexC's user avatar
  • 11
0 votes
1 answer
717 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 ...
ccpizza's user avatar
  • 1,723
7 votes
1 answer
672 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 ...
Saeed Neamati's user avatar
0 votes
0 answers
35 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/"* ]]; ...
Saeed Neamati's user avatar
1 vote
1 answer
176 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:...
The Quark's user avatar
  • 270
0 votes
2 answers
655 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 ...
The Quark's user avatar
  • 270
1 vote
1 answer
805 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; ...
x-yuri's user avatar
  • 3,373
2 votes
2 answers
1k 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 ...
Brad Parks's user avatar
  • 1,679
2 votes
2 answers
1k 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), ...
onlycparra's user avatar
2 votes
1 answer
726 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 ...
Jellicle's user avatar
  • 401
0 votes
1 answer
1k 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/...
Matt's user avatar
  • 11

1
2 3 4 5