Questions tagged [subshell]

A subshell refers to a shell invoked from within a parent shell. It is an example of a child process. Questions with this tag may involve any issue with working with subshells. Please also include tags for platform or shell language.

Most questions with this tag involve the Bash shell, as that is a very popular shell language. However this could relate to any platform, command-line application, or shell that allows the creation of subshells.

295 questions
1
vote
0 answers

Bash script exits unexplained in an if-condition

I searched for hours already, but can't find anything regarding this: I have a bash script that tunnels via ssh from my PC to a Pi to test certain files and if certain conditions are met. I recently added a function which tests the BLE functionality…
Jojo
  • 13
  • 3
1
vote
1 answer

Script that affects the interactive shell that launched it

I wrote this script to be able to quickly go to n:th directory in an ls output: #!/usr/bin/env bash # cd to the nth directory in a list as produced by ls cd $( ls | head -n$1 | tail -n1 ) I named it cde and made it executable (it's in my $PATH) so…
Anton
  • 365
  • 2
  • 12
1
vote
1 answer

How can I make one bash subshell exit the main calling shell script?

Having the following bash script: #!/bin/bash set -e function foo() { # commands that might fails and I want to exit my script ... echo "result I need as output" } my_var=$(foo) echo "I don't want this if there is an error inside…
Gabriel Petrovay
  • 20,476
  • 22
  • 97
  • 168
1
vote
2 answers

echo $(command) gets a different result with the output of the command

The Bash command I used: $ ssh user@myserver.com ps -aux|grep -v \"grep\"|grep "/srv/adih/server/app.js"|awk '{print $2}' 6373 $ ssh user@myserver.com echo $(ps -aux|grep -v \"grep\"|grep "/srv/adih/server/app.js"|awk '{print $2}') 8630 The…
Bai Yang
  • 394
  • 2
  • 5
  • 17
1
vote
2 answers

How do I pass subshell results (array) to an SSH command?

Trying it this way: #!/bin/bash myvals=`psql -d mydb -c "select id from table1 where 't'"` ssh user1@host1.domain.tld "for i in $myvals; do echo \$i >> values; done" As long as psql returns just one value, it works fine. But if its several values,…
Brewer
  • 23
  • 6
1
vote
0 answers

Why does command grouping with curly brackets cause two bash shells to be spawned?

Consider the following command $ bash -c " sleep 10000 | sed 's/ Something//g '" The process tree due to that command looks like this; \-+= 69771 hbaba -bash \-+= 39225 hbaba bash -c sleep 10000 | sed 's/ Something//g ' |---…
Hakan Baba
  • 1,897
  • 4
  • 21
  • 37
1
vote
1 answer

bash-looping through file arguments using subshell

I'm having some trouble with iterating through my script within my subscript. The problem seems to be that one i run it, it only iterates over the first index of my $@ list. I'm thinking it stops and waits for a change before it continues to the…
ruubel
  • 153
  • 1
  • 10
1
vote
1 answer

Bash variable not decrementing in a pipeline

The variable x in the first example doesn't get decremented, while in the second example it works. Why? Non working example: #!/bin/bash x=100 f() { echo $((x--)) | tr 0-9 A-J # this also wouldn't work: tr 0-9 A-J <<< $((x--)) f } f Working…
FranMowinckel
  • 4,233
  • 1
  • 30
  • 26
1
vote
1 answer

Why sometimes a DB2 connection in a subshell is not identified

I am writing the following script: #!/bin/bash db2 connect to andres a=$(db2 connect) echo $a b=$(db2 connect && echo $?) echo $b c=$(db2 connect ; echo $?) echo $c d=$(db2 connect) echo $d What I am doing is to execute multiples commands…
AngocA
  • 7,655
  • 6
  • 39
  • 55
1
vote
1 answer

how to force subshell on level 2 be in own pgid?

Command below prints pid of subshell and subshell of subshell: $ ( ( echo $BASHPID )& echo $BASHPID )& sleep 1 [1] 9885 9885 9887 [1]+ Done ( ( echo $BASHPID ) & echo $BASHPID ) Now command below is more complicated, but it…
noonex
  • 1,975
  • 1
  • 16
  • 18
1
vote
0 answers

stderr and stdout of do-release-upgrade

Hy. I was trying to redirect stderr and stdout of 'do-release-upgrade' command with the following command, sudo do-release-upgrade >>logfile 2>&1 It does only redirect few lines because do-release-update invoke a subshell. So the contents of the…
arif
  • 579
  • 1
  • 7
  • 21
1
vote
1 answer

Can an approach be found to do set -e/ERR subshell trapping immune to && and || restrictions?

While the Bash man page states that: The ERR trap is not executed if the failed command is ... part of a command executed in a && or || list ... I hoped that code in a subshell would be in a different context and would not be subject to the above…
Steve Amerige
  • 1,309
  • 1
  • 12
  • 28
1
vote
2 answers

Parentheses for subshell don't work when stored in a variable

The command: ( echo 1 ) works fine when I input it in the command line but if I store it as a variable and call it, it gives the error: (echo: command not found Code: input="( echo 1 )" $input Why doesn't it evaluate the parentheses the same way…
1
vote
1 answer

How to set bash trap again in trap code?

I have a bash function that is called must be called by an EXIT trap after the first time that it is called. The function sets the trap again to fire as soon as the function exits. echo 0 > .i function launchNextExperiment { ( # Run in nested…
Niel de Wet
  • 7,806
  • 9
  • 63
  • 100
1
vote
3 answers

Avoid subshell from pipe on dash

I have this example code: find "$1" ! -regex "$regex" 2>/dev/null | while read line ; do a="$line" done echo ("$a") # prints nothing because of subshell I need: Workaround for subshell to make $a visible outside (in global scope) To NOT use…
Mára Toner
  • 302
  • 2
  • 16