Questions tagged [subshell]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
-1
votes
0answers
23 views

output of while loop piped to tee and variable missing

It seems that I have faced a famous problem that "pipes spawn subshells" since it has been asked several times, but in my case it is a bit different so I couldn't use their solutions for my problem. I'...
1
vote
0answers
33 views

Is job-control really meant to be supported in subshells and scripts?

Small premise I use subshells quite often to perform operations that involve changing the Shell Execution Environment, so as not to affect the main shell. I do it often from an interactive shell, and ...
0
votes
2answers
46 views

Set shell variable then invoke subprocess fails

I was playing with getopts, which has a shell variable OPTERR that influences its behavior. I wanted to change the value of OPTERR and invoke getopts on the same line, to affect the behavior of ...
5
votes
2answers
66 views

How to handle async subshell exit

Say I have this: set -e; ( docker stop notifier-server docker rm -f notifier-server exit 1 # explicitly exit with non-zero ) & wait; echo 'we are here now' will we always get to ...
0
votes
2answers
38 views

Precedence of subshells in relation to redirection?

Shell: GNU BASH I've been using the following command for a while now: echo "$(fmt -w 50 < foo.txt)" > foo.txt Without any issues. However, when I try: fmt -w 50 < foo.txt > foo.txt ...
9
votes
2answers
466 views

Why does a brace command group need spaces after the opening brace in POSIX Shell Grammar?

TL;DR: Why does POSIX brace group need spaces after { reserved word but subshell doesn't after reserved word (? POSIX shell grammar defines brace group and subshell as follows brace_group : ...
24
votes
3answers
4k views

How can I detect if I'm in a subshell?

I'm trying to write a function to replace the functionality of the exit builtin to prevent myself from exiting the terminal. I have attempted to use the SHLVL environment variable but it doesn't seem ...
0
votes
0answers
51 views

Run script in background using `&` or `()&`?

If I want to run a script in background, for example a.sh, should I run ./a.sh & or should I put it in a subshell then background it? (./a.sh) & I test both and don't see difference, both ...
2
votes
1answer
38 views

Bash script to count number of executions instances does not work

I am writing a script that detects if there is any instance of it already running on my Linux machine and shows on screen the number of instances. The content of the "detect_itself.sh" script is: #!/...
1
vote
1answer
35 views

Is it possible for sendmail to not use all the output from a bash subshell piped to it?

( GNU bash, version 4.2.50(1)-release (powerpc-ibm-aix6.1.2.0)) ( echo "a" echo "b" echo "c" )| sendmail -F "Sender" recipients@a.com This runs inside a .sh file, and the log (when ran with the -x ...
-2
votes
2answers
34 views

Identifying shell script's subshell number from within a script itself

I am accidentally caught in a desire to reveal the subshell number (BASH_SUBSHELL) from within the script itself, but i get subshell 0 Here is the script's line echo "Operated from subshell: $...
0
votes
1answer
37 views

Exported Date Variable not appearing in subshell

I'm currently working on creating 2 bashes with two different scripts which result in two files named with the same timestamp for easy comparison/processing later. The date variable gets created in ...
1
vote
1answer
84 views

Terminating infinite-loop script embedded in tmux status bar

I have an infinite while loop bash script embedded in tmux status bar. In ~/.tmux/conf: set -g status-right '... #(path/loop.sh) ...' Tmux seems to run this script with sh -c. After tmux started: $ ...
3
votes
1answer
471 views

Process substitution inside a subshell to set a variable

I'm trying to run a script remotely and use its standard output to populate a variable. I'm doing this to avoid temporary files. Here's the pattern I'm trying: var=$(bash <(curl -fsSkL http://...
21
votes
3answers
2k views

How does this script ensure that only one instance of itself is running?

On 19 Aug 2013, Randal L. Schwartz posted this shell script, which was intended to ensure, on Linux, "that only one instance of [the] script is running, without race conditions or having to clean up ...
0
votes
1answer
77 views

How can I stop a child process of a subshell (as per SIGSTOP) before the subshell exits?

In bash, when running ( sleep 123 &), the sleep 123 process will continue running, when the subshell exits. How can I stop the sleep 123 process before its parent subshell exits? I'm trying to ...
0
votes
1answer
63 views

Confused why script does not exit

I have this script verbatim: #!/usr/bin/env bash handle_json(){ while read line; do cat <<EOF {"@json-stdio":true,"value":{"mark":"$1","v":"$line"}} EOF done; } ( echo; echo; echo '...
8
votes
2answers
2k views

How can I get the pid of a subshell?

How can I get the pid of a subshell? For example: $ echo $$ 16808 This doesn't work, because the original shell expands $$: $ ( echo $$ ) 16808 Why does single quoting not work? After the ...
0
votes
2answers
54 views

“Globally Store” PID of file started from bash script

I'm compiling a latex file in a script and then I'm trying to either open a new pdf reader window or reload the current one (llpp being my reader), so after compiling my script does pdflatex $...
7
votes
1answer
825 views

exit not terminating the script [duplicate]

exit doesn't terminate the script when error is called.. output Error: Could not resolve localhost after exit script #!/bin/sh resolve_ip (){ if [ -z "$1" ]; then host="localhost" ...
1
vote
1answer
94 views

Weird behavior in zsh random generator

I have this script to generate $1 digit(s) of mixed random char from the defined charlists as arrays. #!/bin/bash charlist1=(a b c d e f g h i j k l m n o p q r s t u v w x y z) charlist2=(0 1 2 3 4 ...
0
votes
1answer
60 views

Catch result of sub-process in Z-shell

AAA <options>; CCC AAA <options> AAA, CCC are different commands. Here we're in an zsh script - for example: vi script.zsh <...> AAA <options>; CCC AAA <options> <.....
2
votes
1answer
513 views

VNC clipboard not working

If I uncomment the two lines in my ~/.vnc/xstartup that enable 'normal' desktop environment for VNC, the vncconfig -iconic & command is ignored: #!/bin/sh # Uncomment the following two lines for ...
2
votes
0answers
51 views

How to make a script being called inside another script in a ssh command? [closed]

I have a ssh calling a script in a server, named /opt/scripts/script1.sh. sshpass -p 'password' ssh user@ip /opt/scripts/script1.sh (I know sshpass is not secure but there's a reason for that in ...
1
vote
1answer
74 views

Export vars env to tmux's window/session/pane's env

Is there any way to export/upgrade list of variable env from a file or something to tmux's window/session/pane? Here I have to manually source .bashrc to update new var env to each tmux's pane.
0
votes
1answer
239 views

Run bash subshell with commands as other user and not return to parent shell

As part of my quest to preserve bashrc and vimrc when switching to non-root users, I plan on making some super ugly aliases to switch to other users, immediately execute some commands, and then drop ...
1
vote
1answer
403 views

How to pass PID of one process to another process within the same shell script?

Okay, so this might be a very silly question and I don't write shell scripts too often. I'm trying to start 3 processes in the background, one after the another within a shell script, for example: #!/...
1
vote
2answers
179 views

Background process of subshell strange behaviour

I wonder why some similar bash commands behave the way they do. I have a bash script foo: #!/usr/bin/env bash while true do echo "reading" read data echo $data echo "stderr msg" >...
1
vote
2answers
2k views

How does (echo 'text' ; cat file.txt ) > new file.txt actually work? [duplicate]

So, here's a simple code: (echo "Some text to prepend"; cat gero.txt) > file.txt And I can't really grasp the mechanics of this code. So basically gero.txt is an already existing file, we create ...
1
vote
2answers
451 views

Difference between pushd/popd and sub-shell+cd

I'm trying to understand if there are any benefits of using: pushd my_dir make all # ... or something else popd vs ( cd my_dir make all # ... or something else ) or is it merely a ...
48
votes
3answers
8k views

Is $() a subshell?

I understand the subshell syntax to be (<commands...>), is $() just a subshell that you can retrieve variable values from? Note: This applies to bash 4.4 based on different wording in their ...
0
votes
1answer
541 views

Setting bash flags in subshells - does it affect parent shell?

I have this in a bash script: ( set -o pipefail echo "foobar" | bash set +o pipefail ) do I need to reset pipefail setting, or can I just omit that line? In other words, does the pipefail ...
1
vote
2answers
93 views

Is it possible to findout if our command is sourced from a script or manually typed on command line?

We have an implementation to set environment for a particular tool with our custom command. Eg: custom_command tool_name This command logs the invokation time, user and tool_name in a log file. And ...
0
votes
1answer
286 views

grabbing exit code of background process/subshell

I am wondering if there is any difference between: long_running.sh & pid=$! and ( long_running.sh ) & pid=$! will the pid be capture consistently in both cases, or will there be some ...
2
votes
1answer
912 views

Compare command output inside if statement without subshell

According to https://www.gnu.org/software/bash/manual/bashref.html#Command-Grouping Placing a list of commands between curly braces causes the list to be executed in the current shell context. But ...
4
votes
1answer
763 views

Why (…) doesn't spawn a new child process when run in background?

After running the command { sleep 5; } & output of ps is (output 1) PID TTY TIME CMD 972 ttys000 0:00.27 -bash 2556 ttys000 0:00.00 -bash 2557 ttys000 0:00.00 ...
4
votes
1answer
1k views

Syntax error in nohup (…)

I am trying to test the nohup command in the bash shell by executing a command in the background and logging out and back in again and expecting the output of the command to be in the nohup.out file. ...
1
vote
1answer
243 views

Child process - listen for event when parent dies

I have a detached child process. It knows its parent's pid. Is there some way on both MacOS and Linux that I can listen for when the parent process exits, from the child process? I need to make a ...
1
vote
1answer
69 views

Are processes run from Bash run in a “sub shell”?

When I run an executable (like a.out) from a Bash shell, is that executable run in some kind of "sub" shell, i.e. different from the shell at which I'm typing? I'll try illustrate my question with an ...
2
votes
1answer
281 views

Run subshells in parallel and collect exit codes

I have the following, I thought it ran the subshells in parallel, but it actually seems to run them in series, and I cannot figure out why: #!/usr/bin/env bash set -e; set -m # allow for job control ...
1
vote
2answers
516 views

How to execute ant scripts in a differing directory to a shell script?

I'm writing a shell script to automate an elaborate build process. It includes execution of multiple ant scripts located in different directories. The shell script is in my home directory, while the ...
6
votes
1answer
5k views

Exporting a variable from inside a function equals to global export of that variable?

I use Ubuntu 16.04 with the native Bash on it. I'm not sure if executing #!/bin/bash myFunc() { export myVar="myVal" } myFunc equals in any sense, to just executing export myVar="myVal". Of ...
2
votes
1answer
265 views

BASH subprocess using SSH in script?

I want to do something like the following in a script $ ssh host1 "$(command)" However, the sub processes gets executed before the SSH connection is established because if I do the following $ ssh ...
3
votes
5answers
171 views

How to execute many files in the same directory in a minimal, DRY, pretty way?

I use Ubuntu 16.04 and I have a localization file, a file that executes many files, that I got after downloading my own GitHub project to my machine. This file contains a Bash script, and is named ...
9
votes
3answers
2k views

What is the exact difference between a “subshell” and a “child process”?

According to this and this, a subshell is started by using parenthesis (…). ( echo "Hello" ) According to this, this and this, a process is forked when the command is started with a & echo "...
2
votes
1answer
597 views

Variable inherited in the subshell without exporting in the main shell [duplicate]

I am having the below shell script var="this is a test" ls -ltr| while read file do echo $var done echo $var I am getting the below output: this is a test this is a test this is a test How ...
2
votes
3answers
1k views

Silently kill subshell?

I want to implement something like this Q/A but for a sub-shell. Here is a minimal example of what I'm trying: (subshell=$BASHPID (kill $subshell & wait $subshell 2>/dev/null) & sleep ...
8
votes
1answer
395 views

Why does exiting with a stored returncode of a nested command result in different returncodes in Dash and Bash?

Running bash -c 'bash -c "echo test1; exit 1;" &> /tmp/x; buildresult=$?; tail -n 100 /tmp/x; exit $buildresult;' results in test1 being printed to console and echo $? to print 1 which in my ...
1
vote
2answers
140 views

How to get output of variable with line breaks?

I want to get onre output of top to variable for future processing. I tried: top=$(top -n1) top=$(top -n1 -w80) top=$(top -n1 -b -w80) top=$(COLUMNS=100 top -b-n1) When I run echo $top then I see ...
6
votes
1answer
623 views

Why is the output on this ping outside the subshell?

I have a simple script that checks if an interface is connected: connected=$(ping -I $1 -qc 1 8.8.8.8 \ && echo "connected" \ || echo "not connected") echo "$connected" | tail -n1 I'm ...