The Stack Overflow podcast is back! Listen to an interview with our new CEO.

Questions tagged [subshell]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
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 ...
2
votes
2answers
559 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
3k 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 ...
51
votes
3answers
9k 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
679 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
380 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 ...
18
votes
7answers
19k 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 ...
94
votes
2answers
78k 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 (...
11
votes
2answers
3k 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 ...
5
votes
1answer
1k 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
327 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
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
260 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
2answers
593 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 ...
3
votes
5answers
176 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 ...
8
votes
5answers
13k views

How to get subshell's PID in Korn Shell (equivalent of $BASHPID)

In bash you have this handy variable: $BASHPID wich always returns the currently running subshell's PID. How can I get a subshell's PID in ksh? For example see the code below: #!/usr/bin/ksh93 echo "...
8
votes
1answer
7k 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
310 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 ...
5
votes
1answer
9k views

How to trace system calls being invoked after typing command?

This question is relevant to the question of 'what happens when we type a simple command on shell?' I posted earlier. (I thought it would be better to post it separately. But if not, please let me ...
35
votes
2answers
17k views

What happens when we type a simple command on shell? [duplicate]

I have a simple question regarding the execution of a simple command. As per my understanding, when we type a command such as ls in an interactive shell, Shell interprets the command. Shell creates ...
7
votes
1answer
4k views

Flock doesn't seem to be working

I've recently been trying to create a shell script for a minor project, and for some reason, the flock command isn't working for me properly. Whenever I invoke it in a subshell the atomic way and put ...
2
votes
1answer
696 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 ...
47
votes
4answers
30k views

Why does ( exit 1 ) not exit the script?

I have a script, that does not exit when I want it to. An example script with the same error is: #!/bin/bash function bla() { return 1 } bla || ( echo '1' ; exit 1 ) echo '2' I would assume ...
2
votes
3answers
2k 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
407 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
270 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
685 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 ...
30
votes
3answers
22k views

Bash subshell creation with curly braces

According to this, placing a list of commands between curly braces causes the list to be executed in the current shell context. No subshell is created. Using ps to see this in action This is the ...
0
votes
1answer
330 views

can't capture PID from background process started in a sub-shell that's running in a heredoc passed to `sudo su`

I'm trying to capture the PID of a process started in a sub-shell so I can check if the process started correctly, the process in question is a puma app. sudo su - $APPUSER <<-BASH PID="\$(...
8
votes
2answers
3k views

$BASHPID And $$ differ in some cases

I'm reading "BASH pocket guide of Oreilly". It said: The process ID of the current Bash process. In some cases, this can differ from $$. Above explanation , explained $BASHPID variable. ...
4
votes
1answer
1k views

Difference between subshells and process substitution

In bash, I want to assign my current working directory to a variable. Using a subshell, I can do this. var=$(pwd) echo $var /home/user.name If I use process substitution like so: var=<(pwd) ...
1
vote
0answers
1k views

Restoring tty correctly with stty

I'm writing a bash script to read some user input (like a password request), and I'd like to capture the result and pass it through its stdout. (I'm working in a GNU/Linux environment, with recent ...
1
vote
1answer
346 views

“. ~/.bashrc” does not set variables as expected in the calling script

Background I have written a script to install node via nvm. Issue The command . $LOCAL_SHELL_CONFIG_FILE inside install_latest_stable_node() does not run and thus, when it prints the PATHs for node ...
5
votes
2answers
1k views

Is there a simple test for anything printed on stderr in shell/Bash?

I'd like invoke a command inside a shell script for Continuous Integration purposes. Exit status 0 means succes, otherwise failure. I'm writing a wrapper script to run several commands and fail if any ...
0
votes
0answers
16 views

Variable will not stay set after being modified inside of a PIPE while loop [duplicate]

I was writing a script, and ran into a problem where my variable will not stay set when I modify it from inside of a | while loop. To illustrate, I wrote this example: #!/bin/bash thevar="Foo" ...
0
votes
1answer
502 views

Subshell inside quotes

I'm writing a small shell script like this: curl -X POST --header 'Bearer "$(printf user:pass | base64)"' 'https://api.com/v1/auth' To debug I switched to echo: echo 'Bearer "$(printf remote-key-...
6
votes
2answers
8k views

How to get functions propagated to subshell?

Solaris / sh I have a few functions defined in a file which gets loaded via . ./some_file.sh When I start a subshell with sh All my function definitions are lost but when I do env I do see ...
3
votes
4answers
969 views

Bash: escaped quotes in subshell [duplicate]

When I execute the following command: #!/bin/bash while IFS= read -r -d '' file; do files+=$file done < <(find -type f -name '*.c' -print0) echo "${files[@]}" I do not get the same result ...
4
votes
4answers
975 views

How to execute bash (in script) with own .bash_logout file?

I want to execute bash in subshell and when user exits from subshell, I want to execute some other commands (like for saving logs to file). Something like this: run.sh: #!/bin/bash function ...
1
vote
2answers
1k views

Using subshell to pass parameters with spaces

I have a shell called "name_value.sh" that I want to pass parameters generated in a sub-shell. How can I ensure the command receiving parameters from the sub shell retain spaces? NOTE: This is an ...
12
votes
3answers
2k views

Is trap inherited by a subshell?

I tried a following script: #!/bin/bash trap 'echo "touching a file" && touch $FILE' EXIT foo1(){ echo "foo1" } foo(){ echo "foo" export FILE=${FILE:-/tmp/file1} } (...
4
votes
3answers
413 views

Can it be explained; the difference in array behavior between the use of array=$( command ) and array=( $( command ) )?

I understand command substitution. I understand subshells. I do not understand why using a subshell changes the structure of my array. Given this command output: (use of openstack command is not ...
2
votes
0answers
126 views

Why do echo $$, echo $(echo $$) and (echo $$) reveal the same PID? [duplicate]

As far as I know echo $$ shows the PID of the current BASH I am in. I do not know if the expasnsion $(...command...) runs the command in a subshell or the current shell, however echo $(echo $$) ...
0
votes
2answers
117 views

How to login to a remote user account using password and execute a set of commands?

I want to execute commands on a remote machine. The remote user requires password authentication. I am getting following error when using the below sample script, This is the content of sample script ...
4
votes
2answers
3k views

What are all the ways to create a subshell in bash?

There are 2 main ways that I know of so far: Explicitly: wrapping parentheses around a list of commands Implicitly: every command in a pipeline Are there more ways, either explicitly or implicitly, ...
14
votes
2answers
3k views

What is “( set -o posix ; set ) | less ” doing? [duplicate]

On my search for a command to list all shell variables, I somehow realized, that there is a command to list all environment variables, but somehow there is no one to list all shell variables, for ...
4
votes
1answer
651 views

Access variables set inside command substitutions

I wrote a simple script #!/bin/bash -x selentry=`ls -l / | sort ; ts=${PIPESTATUS[0]}` echo $ts but $ts show nothing. How I can display $ts variable, or how I can get exit status code from ...
0
votes
1answer
768 views

Subshell and process substitution

Apologies if this is a basic question - I'm stuck trying to solve a larger problem, and it's come down to how a shell script is invoked - directly (shellScript.sh) or using sh shellScript.sh. Here's ...