Questions tagged [subshell]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
4
votes
1answer
718 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
824 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 ...
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
681 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 ...
5
votes
2answers
1k views

Bash and subshells

From what I gather from the online docs, the following should spawn a subshell for the part of the command embedded with {}: $ bash -c '{ sleep 10; echo "Sleeping process", $$; } & echo $$; ' ...
2
votes
1answer
328 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
531 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
228 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
581 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 ...
2
votes
0answers
241 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
450 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
523 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, ...
8
votes
1answer
13k 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
197 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 ...
7
votes
2answers
10k 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 & ...
1
vote
1answer
420 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
1k 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 ...
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 ...
7
votes
1answer
520 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]+...
1
vote
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 ...
3
votes
2answers
2k views

How to set Cols and Lines for a Subprocess

I have a script that runs a series of scripts numbered 001,002,003,004... etc down to 041 right now, will be more in the future - and these scripts them selves use some cursor control to print a ...
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 ...
5
votes
1answer
667 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 ...
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 ...
0
votes
1answer
744 views

Why don't subshell expressions in ~/.zshenv lead to infinite regress?

I understand that ~/.zshenv gets sourced every time that an instance of zsh starts. This implies that having the following line ~/.zshenv would result in an infinite regress: (echo "$$ $(date)" >&...
1
vote
0answers
307 views

Start a subshell then run commands

Say I have a script that is always run from an interactive shell. I would like this script to launch an interactive subshell that is a replica of the parent (i.e., all environment variables, etc. ...
1
vote
1answer
441 views

Broken pipe error when reading stops

I'm trying to make a quick & dirty bash function to compare the audio content of two FLAC files without decoding them into a temp file. With the flac command line tool I'm decoding each file into ...
0
votes
2answers
638 views

How to execute multiple .command files simultaneously?

I have a folder of .command files (regular task) on Mac OS X , which I would like to execute simultaneously. At the moment, I have been iterating through each file in the directory one by one to ...
16
votes
5answers
10k views

Is this a bug in bash? `return` doesn't quit function if called from a pipe

I'm been having some weird problems with bash lately. While trying to simplify my script, I came up with this small piece of code: $ o(){ echo | while read -r; do return 0; done; echo $?;}; o 0 $ o(){...
3
votes
6answers
2k views

How to run no more than n parallel subshells

I am trying to run subscripts from a main script, but I want to make sure than no more than n subscripts run at the same time. The following simplified example illustrates. Each subscript creates a ...
11
votes
2answers
1k views

Expand subshell before executing it?

I often run commands using subshells, and sometimes would like to have the subshells expanded before I run something.. This way I could verify what I'm doing, and possibly edit what's about to happen ...
2
votes
2answers
2k views

viewing printf after pipe (or subshell?)

Ok, this is hard to distill to a single question because it really depends on how I'm going to troubleshoot it. The following script is meant to pipe mount.cifs commands to a while loop that enters ...
5
votes
1answer
987 views

echo result from subshell in zsh

In zsh, the following 2 commands results differently: a=$(</etc/hosts) && echo $a echo $(</etc/hosts) The 1st prints contents line by line, while the 2nd prints contents as a whole in ...
7
votes
3answers
4k views

Use bash's read builtin without a while loop

I'm used to bash's builtin read function in while loops, e.g.: echo "0 1 1 1 1 2 2 3" |\ while read A B; do echo $A + $B | bc; done I've been working on some make project, and ...
3
votes
1answer
1k views

Bash tab completion fails inside of command substitution

I've been using kill $(pidof ) to terminate programs for a while, and since last night, every time I try to use tab completion inside the $() it throws an error, bash: unexpected EOF while looking ...
0
votes
3answers
76 views

Diff between (x=100) and { x=100; }?

Can some one please explain the below statements? $ x=50 $ (x=100) #here the code executes only with in the subshell. when subshellexecution done then the x value is returned to its original ...
18
votes
2answers
2k views

Why is a variable visible in a subshell?

The Learning Bash Book mentions that a subshell will inherit only environment variables and file descriptors, etc., and that it will not inherit variables that are not exported: $ var=15 $ (echo $...
5
votes
2answers
3k views

Assign Subshell background process pid to variable

I am currently starting a background process within a subshell and was wondering how can I assign its pid number to a variable outside the subshell scope? I have tried many different ways but MYPID ...
5
votes
1answer
371 views

Bash - how to make explicit operator precedence without creating a subshell

I'm sure this is posted somewhere, but I haven't been able to find it. In Bash, how does one specify operator precedence (aka command grouping) without creating a subshell? In most other languages ...
2
votes
1answer
2k views

Change the PS1 prompt in a subshell sh called from parent bash

When I am running an interactive bash session, I sometimes experiment with sh. I call sh directly from this bash session. My question is, how can I change the PS1 prompt of this subshell? I would ...
16
votes
5answers
2k views

Stateful bash function

I’d like to implement a function in Bash which increases (and returns) a count with every call. Unfortunately this seems non-trivial since I’m invoking the function inside a subshell and it ...
2
votes
1answer
2k views

Understanding behavior of subshell and stdout with pipe

Let us say I have .c file and corresponding executable (let us say foo) that takes in some input from stdin and then makes a sys call to /bin/sh. Feeding input to this executable like following: ...
0
votes
1answer
1k views

How to pass a flag from one script to a function in another script

I have a script that installs a bunch of programs (homebrew, git, etc) and I have it log to stdout all the things that its doing along the way, but I want the ability to pass a flag, such as --no-log ...
6
votes
1answer
2k views

When should you use subshells vs `xargs`?

Many simple commands using xargs could be rewritten as commands using subshells. For instance, here is something I was using earlier today, to concatenate the ten largest binaries in /usr/bin, written ...
-2
votes
1answer
1k views

why does the dollar sign cause this command to be executed?

test_script.sh: #!/bin/sh D1="$(dirname $0)" echo $D1 D0="(dirname $0)" echo $D0 What I type on the command line: $ ./test_script.sh Output: . (dirname ./test_script.sh) What significance does ...
13
votes
2answers
1k views

Bash syntax highlighting of subshells

Considering the large amount of questions which turn out to be related to subshells in Bash ("Why doesn't my variable increment in this piped while read loop?"), I just thought it would be very nice ...
1
vote
1answer
720 views

curly braces and subshell

I can't figure it out. As I read in documentaion, {} doesn't create a subshell. However, looks like that sometimes it does: $ unset T; echo "T_bfr=$T"; echo $$; { echo $$; export T=1; }; echo "...
7
votes
2answers
3k views

Behavior of stdin/stdout in conjunction with subshells and cd command

I have the following series of commands: cd / && ls | ( cd /tmp && cat >dumpfile) This series of commands does the following: it creates a file named /tmp/dumpfile. This file ...