Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [subshell]

The tag has no usage guidance.

-2
votes
2answers
26 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
25 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 ...
3
votes
3answers
2k views

Capture the output of a shell function without a subshell

I have rbenv (ruby version manager) installed on machine and it works like that: $ rbenv local 2.3.1 Writing to stdout the local version of my ruby. I want to rescue this version and declare it in a ...
1
vote
2answers
982 views

cd in bash script without alias, function, source [duplicate]

I've written a bash script which should cd me to a directory. My problem is that only the directory in the subshell changes. I've read of many similar problems to this, but I want to know if there ...
3
votes
3answers
3k views

Set environment variable for subshell

I know that I can run a command with an environment variable like this: FOO=bar mycommand I know that I can run commands in a subshell like this: (firstcommand && secondcommand) But can I ...
1
vote
1answer
51 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: $ ...
9
votes
3answers
1k 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 "...
3
votes
1answer
270 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://...
3
votes
1answer
675 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 ...
1
vote
1answer
321 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: #!/...
0
votes
2answers
347 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 ...
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
75 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
61 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 '...
1
vote
2answers
144 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" >...
3
votes
2answers
801 views

CTRL C behavior in hierarchy of shells

I open a shell (like sbt or node debug from my bash). Then inside this shell, I open another one (using scala or node repl respectively). Now I want to close the last shell and come back to the first ...
1
vote
1answer
66 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 ...
27
votes
6answers
20k views

exit shell script from a subshell

Consider this snippet: stop () { echo "${1}" 1>&2 exit 1 } func () { if false; then echo "foo" else stop "something went wrong" fi } Normally when func is ...
4
votes
2answers
1k views

How can I get the pid of a subshell? [duplicate]

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 ...
4
votes
2answers
800 views

bash syntax - wrapping command inside sudo :: tail logs until string found with timeout using Terraform

Goal: using sudo execute a one line cmd to tail a log until a string is found and then exit 0. If the string is not found within a given timeout, exit anything but 0. Attempted Solution 1: ...
0
votes
1answer
193 views

Subshell for Bash Script [closed]

Sub-shell commands must be execute automatically within bash scripts. It runs automatically then executing the sub-shell within the bash but it requires human interaction I want to execute it ...
0
votes
3answers
133 views

grep command on shell script

I have a log file and I am trying to write a shell script to find multiple strings in a file on multiple lines. For example if I see the phrases "class=com.comcast.parker....
0
votes
2answers
49 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 $...
6
votes
2answers
1k views

What is the overhead of using subshells?

Hopefully this question is not too generic. I am very new to shell scripting and I come from a computer architecture/non-scripting programming background. I have noticed on the scripts at my work that ...
7
votes
1answer
659 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
71 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
55 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> <.....
1
vote
1answer
154 views

Subshells not working

I want to run several commands in parallel, but I have no idea why the following code piece does not work. #!/bin/bash ( ping 8.8.8.8 ) ( ping 192.168.0.1 ) It completely ignores the second ping ...
5
votes
2answers
459 views

Why bash does not spawn a subshell for simple commands?

Consider the two following bash commands. One creates a subshell and the other does not. No subshell $ bash -c "sleep 10000 " pstree output: bash,100648 └─sleep,103509 10000 With subshell $ ...
2
votes
1answer
312 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 ...
18
votes
1answer
2k views

In zsh, difference between cat <(cat) vs cat | cat vs cat =(cat)?

I expected cat <(cat) and cat | cat to do the same thing: copy lines from stdin to stdout. My understanding was that both would execute a cat in a subshell, redirect the subshell cat's stdout to a ...
2
votes
0answers
47 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
0answers
50 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
154 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 ...
9
votes
2answers
5k views

How to make a variable from a subshell available in the parent shell

I've written a quick-and-dirty script to time some reports from a web service: BASE_URL='http://example.com/json/webservice/' FIRST=1 FINAL=10000 for report_code in $(seq 1 $FINAL); do (time -p ...
0
votes
0answers
79 views

Environment variables in Subshell

I try to execute ImageMagick on MacOS in an Ant script. Although there's no problem while running in the Terminal, Ant throws an error... [exec] dyld: Library not loaded: /ImageMagick-7.0.7/lib/...
1
vote
2answers
462 views

How to control a backgrounded subshell?

If I run the following command at a bash prompt: (for i in {1..100}; do echo $i; sleep 1; done) & It will start counting and immediately return me to my prompt. I can continue working normally, ...
1
vote
2answers
1k 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 ...
48
votes
3answers
7k 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
439 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
212 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 ...
16
votes
7answers
14k views

Collect exit codes of parallel background processes (sub shells)

Say we have a bash script like so: echo "x" & echo "y" & echo "z" & ..... echo "Z" & wait is there a way to collect the exit codes of the sub shells / sub processes? Looking for way ...
24
votes
3answers
12k views

Rule for invoking subshell in Bash?

I seem to misunderstand the Bash rule for creating a subshell. I thought parentheses always creates a subshell, which runs as its own process. However, this doesn't seem to be the case. In Code ...
82
votes
2answers
66k views

Do parentheses really put the command in a subshell?

From what I've read, putting a command in parentheses should run it in a subshell, similar to running a script. If this is true, how does it see the variable x if x isn't exported? x=1 Running (...
12
votes
2answers
2k views

Is a sub-shell the same thing as a child-shell

There are this two names: a subshell and a child-shell. Yes, a child process will be started by any of this: sh -c 'echo "Hello"' ( echo "hello" ) echo "$(echo "hello") echo "hello" | cat Are all ...
4
votes
1answer
619 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 ...
2
votes
1answer
220 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 ...
4
votes
1answer
914 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
222 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 ...