Questions tagged [subshell]
Use where a shell is run inside a shell (nested shells)
246 questions
0
votes
2
answers
145
views
Process sed capture group with a bash function before replacement: "sh: 1: <bash function>: not found"
So, I was playing around with this answer, when I found that neither
printf_stdin () {
read input
printf "$input"
}
sed "/lorem ipsum foobar/ {
s/'/'\"'\"'/g
s/\...
5
votes
1
answer
291
views
How to wait for subshells to finish from the outer shell?
If do this on my bash terminal:
(
(sleep 0.5 && echo 'first command, second result' &);
(echo 'second command, first result' &);
)
then I see:
second command, first result
...
4
votes
2
answers
349
views
Deferred variable expansion including in a subshell
I have a command that takes always the same initial parameters. I can create a variable to capture these parameters. Also, I need to pass the output of a subshell running the same command to the ...
4
votes
4
answers
551
views
How to wait for background commands that were executed within a subshell?
Given this code:
#!/bin/bash
set -euo pipefail
function someFn() {
local input_string="$1"
echo "$input_string start"
sleep 3
echo "$input_string end"
}
function ...
0
votes
2
answers
103
views
How to temporarily substitute the login shell for running a shell command/subprocess?
My login shell is Fish, but I would like to execute a shell command (apt install ...) as if my login shell was Bash.
Is it possible to make a command/subprocess believe that my login shell is /usr/bin/...
4
votes
0
answers
100
views
Why is `fc` output different through a pipe in a subshell?
Why does echo "$(fc -l -1)" show the previous command, but echo "$(fc -l -1 | cat)" show the current command?
$ testfc () {
> echo "$(fc -l -1)"
> echo &...
0
votes
0
answers
38
views
bash subshell execution behaves unexpectedly [duplicate]
I have a script which is supposed to fetch 2 URLs sequentially:
#!/bin/bash
wget_command='wget --restrict-file-names=unix https://www.example.com/{path1,path2}/'
$($wget_command)
echo $wget_command ...
5
votes
2
answers
345
views
Duplicate stdout to pipe into another command with named pipe in POSIX shell script function
mkfifo foo
printf %s\\n bar | tee foo &
tr -s '[:lower:]' '[:upper:]' <foo
wait
rm foo
This is a working POSIX shell script of what I want to do:
printf %s\\n bar is symbolic for an external ...
1
vote
0
answers
93
views
What's the logic in exiting early on failure in blocks and subshells in Bash? [duplicate]
In Bash blocks {} and subshells () the exit early doesn't work if there is an OR condition following it. Take for example
set -e
{ echo a; false; echo b; } || echo c
prints
a
b
and
set -e
{ echo a; ...
5
votes
1
answer
351
views
How to determine what is opening tmp files when I invoke a subshell with ksh
I'm experiencing extremely sluggishness in opening subshells (by using ` ` or $( ) command substitutions in scripts) while in ksh on some Linux servers. The same problem does not exist in sh or any ...
1
vote
0
answers
26
views
Why is the first sub-command of my remote command not executing or not affecting later sub-commands? [duplicate]
This command is a simplification of the problem I've come across:
ssh grinder1h.devqa sh -c "cd /etc/ssh && pwd"
This command gives an output of "/home/fetch", which is ...
3
votes
0
answers
197
views
Understanding piping to a unix domain socket in the background
I have a Debian 12 (bookworm) host where I run three VMs using QEMU / KVM. To simplify VM management, each VM has a QEMU monitor socket. These sockets are /vm/1.sock, /vm/2.sock and /vm/3.sock. Among ...
0
votes
1
answer
83
views
How to exit a shell if the subshell exit with an error [closed]
there is a script, 1.sh. 1.sh starts 1a.sh and then 1b.sh.
But how to exit all scripts, how to exit 1.sh and how to not start 1b.sh if 1a.sh breaks with an error?
-2
votes
1
answer
95
views
How does bash <command-argument> work?
About bash
For a new tty when is executed the echo $SHLVL command it displays 1 as expected. Now, if in the same tty is executed the bash command and later again the echo $SHLVL command it displays 2. ...
0
votes
1
answer
445
views
Is it possible to give an existing Flatpak application permission to run another Flatpak?
You can give a Flatpak permissions to access files/folders outside of its sandbox using the techniques described in this Ubuntu Stack Exchange QA.
But is it possible to give an existing Flatpak ...
2
votes
1
answer
123
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? ...
1
vote
1
answer
54
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 ...
4
votes
1
answer
404
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 ...
3
votes
2
answers
496
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,...
0
votes
2
answers
236
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 ...
0
votes
1
answer
127
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 ...
2
votes
1
answer
123
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 (...
0
votes
1
answer
108
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;
...
1
vote
1
answer
434
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 ...
0
votes
2
answers
105
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 <()
...
0
votes
1
answer
739
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 ...
-1
votes
1
answer
74
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:...
-1
votes
2
answers
318
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 $() ...
1
vote
1
answer
189
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&...
3
votes
1
answer
309
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
$
Debian ...
4
votes
2
answers
1k
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
745
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
3k
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 ...
0
votes
1
answer
180
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,...
10
votes
2
answers
2k
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 ...
0
votes
1
answer
2k
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; ...
1
vote
1
answer
3k
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=`...
0
votes
1
answer
637
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 ...
1
vote
1
answer
196
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[@]}" $!)
...
1
vote
2
answers
1k
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
580
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 ...
0
votes
0
answers
51
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
54
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
337
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
583
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
141
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 ...
0
votes
1
answer
522
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 ...
1
vote
1
answer
229
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
...
0
votes
2
answers
271
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
355
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
...