Questions tagged [subshell]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
5
votes
2answers
520 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 $ ...
0
votes
1answer
288 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="\$(...
4
votes
1answer
806 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) ...
5
votes
3answers
4k 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
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
327 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 ...
7
votes
1answer
3k 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 ...
5
votes
2answers
871 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
468 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-...
3
votes
4answers
824 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 ...
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 ...
4
votes
3answers
350 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
125 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
99 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 ...
13
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 ...
0
votes
1answer
679 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 ...
4
votes
2answers
2k 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, ...
3
votes
2answers
1k views

Get PID and return code from 1 line bash call

Imagine we have a long command sleep 10 (for instance). We would like to execute it on another server using python (the ssh library paramiko to be specific). I need a 1 line command that starts the ...
3
votes
1answer
587 views

Kill job in subshell with set -m

I have the following bash script: #!/bin/bash set -m ( (bin/pnvd &> /dev/null; kill 0) & sleep 2 perl integration-test/fuzz-test.pl || kill 0 kill %1 ) The first three ...
16
votes
7answers
16k 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 ...
2
votes
1answer
275 views

Subshell does not create a separate process

I have a ./scr script. abc@~ $ cat scr #!/bin/bash ps echo '-------' echo "$(ps)" abc@~ $ My goal is to find out how are the subprocesses created. As far as I know, the $(...) part should create a ...
4
votes
1answer
516 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 ...
3
votes
1answer
473 views

zsh: command substitution does not inherit stdin from its parent

Consider the following command: seq 5 | grep $(tail -n1) <(seq 9) When running it in zsh1: tail: error reading 'standard input': Input/output error Now running the same in bash, it outputs: 5 ...
-2
votes
1answer
149 views

How to refer variable defined in sub shell in parent shell [duplicate]

How to refer variable defined in sub shell in parent shell. command grouping is difficult in my case. What would be the alternative.
5
votes
4answers
578 views

Check whether console application is really exited

Suppose that I have started a console application which finishes, then shell prompt brings up again. But how can I be sure that it's the real command prompt? What if, for example, the application is a ...
0
votes
1answer
206 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 ...
4
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 ...
2
votes
0answers
222 views

Tmux status bar flashes command from #() before displaying result

I currently use the following command to display the current git branch or mercurial bookmark for the pane's working directory. It works correctly as far as I can tell, but just before it computes ...
1
vote
1answer
403 views

Is the command in a process substitution invoked in a subshell?

From the bash manual Process substitution is supported on systems that support named pipes (fifos) or the /dev/fd method of naming open files. It takes the form of <(list) or >...
0
votes
1answer
432 views

Preserve spacing and newline characters while using 'subshell' method [duplicate]

by describing my issue, I refer to the following code: #!/bin/bash #fileName: subMet.sh cmd="$(ls -l | cat -n)" echo $cmd Then, in the script above I used double quotes to assign a value to cmd, ...
4
votes
2answers
844 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: ...
8
votes
1answer
11k views

How can I set environment variables for a program executed using `nohup`?

(I'm editing an existing Bash script, so I'm probably making a silly mistake here...) I have a shell script that saves a command with an environment variable as its argument like this: COMMAND="mvn ...
4
votes
2answers
153 views

How to put sub shell and related commands in background?

Run a sequence commands in a sub shell like this: (git pull;git rebase develop)&>/dev/null I think this will put the sub shell and all children processes created by it in background, and ...
4
votes
4answers
926 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
1answer
382 views

Bizarre variable behavior in `while` loops [duplicate]

Can someone explain to me what's going on here? Script: #!/bin/sh SKIP="unity-launcher|unity-panel|unity-dash|Hud|XdndCollectionWindowImp|Desktop" WINS=() wmctrl -l | grep -Ev " (${SKIP})" | cut -d \...
2
votes
2answers
843 views

Alternative to process pipeline

Shell script on which I am working is having one SQL query which fetches multiple column and multiple rows from DB: get_names() { $ORACLE_HOME/bin/sqlplus -s usr/pwd <<EOF SELECT id,...
7
votes
1answer
2k views

set -e in a subshell

I thought set -e had the same effect on subshells as on top-level shell. Apparently, it does not. This: ( set -e false true ) || echo false1 bash -ec ' set -e false true ' || echo ...
6
votes
2answers
8k views

Putting subshell in background vs putting command in background

I have two bash scripts that try to check hosts that are up: Script 1: #!/bin/bash for ip in {1..254}; do ping -c 1 192.168.1.$ip | grep "bytes from" | cut -d" " -f 4 | cut -d ":" -f 1 & ...
7
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 ...
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
2answers
4k views

Scope of an exported variable in shell

Say I have a script where I have exported a variable as seen below: #!/bin/bash foo(){ eval export one=1 } foo1(){ eval export two=2 } (foo) foo1 echo "one=$one" echo "two=$two" However I ...
6
votes
1answer
468 views

Cannot grep jobs list when jobs called in a function

I can grep the output of jobs, and I can grep the output of a function. But why can't I grep the output of jobs when it's in a function? $ # yes, i can grep jobs $ jobs [1]+ Running vim [2]+...
0
votes
4answers
2k views

How to run subshell commands over SSH?

I have a cluster of computers which are connected via passwordless SSH and I have written a utility which runs a command over the entire cluster. The way it does it is by picking up machines present ...
1
vote
1answer
172 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
3answers
2k views

On `fork`, children processes, and “subshells”

This post is basically a follow-up to an earlier question of mine. From the answer to that question I realized that not only I don't quite understand the whole concept of a "subshell", but more ...
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 ...
0
votes
3answers
134 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....
1
vote
3answers
1k views

Can I redirect the output of a program to cat with a pipe?

I have a folder with three files and basic contents: $ tail * ==> file1 <== file 1 contents ==> file2 <== file 2 contents ==> file3 <== file 3 contents I would like to see the ...
5
votes
1answer
597 views

Why is subshell created by background control operator (&) not displayed under pstree

I understand that when I run exit it terminates my current shell because exit command run in the same shell. I also understand that when I run exit & then original shell will not terminate because ...