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
2
votes
1 answer

Why does a subshell of an interactive shell run as an interactive shell?

With my Bash session, I run this command: (echo $$ $BASHPID $-) and I get 22108 25602 himBH So, my subshell runs as an interactive shell. If I try to run the same command in background (echo $$ $BASHPID $-) & I get the same output. Why does the…
codeforester
  • 39,467
  • 16
  • 112
  • 140
2
votes
2 answers

Sub-shell process not be able to access variables and functions defined in parent shell

I have 3 scripts in the same directory, please find below contents of x.sh, y.sh and z.sh :- x.sh :- xData="DataOfX" function xInit(){ echo "xInit : data of a >$xData<" } y.sh :- . x.sh xInit sh z.sh zInit z.sh :- function zInit(){ echo…
mogli
  • 1,549
  • 4
  • 29
  • 57
2
votes
2 answers

(BASH) Passing a loop variable into command that will be stored in a variable

I am trying to iterate through a line-by-line list of file ID strings (testIDs.txt) and pass them to an AWS command. The command should utilize the loop variable, and the output of the command should be stored in the "folder" variable. Whenever I…
2
votes
1 answer

Redirect sterr in subshell to stdout in current shell in bash script

I have a subshell carrying out a function: local thing=$( doFunc ) doFunc sends logging output to stderr (2) and 'thing' gets assigned to doFunc's output on stdout (1). How can I run this line, but print stderr from the subshell to stdout in the…
Jordan Mackie
  • 2,264
  • 4
  • 25
  • 45
2
votes
0 answers

SIGINT propagation for background process vs for subshell

I have two programs in two files that I run with bash: The first: (sleep 100) & wait The second: sleep 100 & wait If I send a SIGINT to the first program, it also kills my sleep command. But for the second the sleep command remains and isn't…
James Pinkerton
  • 161
  • 2
  • 14
2
votes
3 answers

Preserve return value and run not from subshell

I'm calling some function which sets VARIABLE to some value and return another value. I need to preserver the value of VARIABLE and assigning the function's return value to another VAR. Here is what I tryied: bar() { VAR="$(foo)" …
Some Name
  • 8,555
  • 5
  • 27
  • 77
2
votes
2 answers

Is using builtins to enhance performance negated by gratuitous use of subshells?

I'm writing a script where I need to make sure a string contains a comma. If it doesn't I need the script to exit. Consider the below, where my intent is to only use builtins to enhance performance: #!/bin/sh check_for_commas='This string must…
Harold Fischer
  • 279
  • 1
  • 9
2
votes
1 answer

How to wait for results from a subshell in Bash?

I'm writing a helper function in Bash, whose task is to read some values from STDIN (one per line, like in ls -1) and then ask the user to pick one interactively. Before, I would use dmenu, but I wanted to make it more robust and provide for cases…
Wojciech Gac
  • 1,538
  • 1
  • 16
  • 30
2
votes
2 answers

Retrieve the first/last line of subshell stdout

If I have a subshell command: output="$(runfoo)"; is there way to store only the last line of the output from runfoo into the variable output? Or perhaps only the first line?
user7898461
2
votes
1 answer

Is ( ... ) a subshell in this code or something else?

In this POSIX shell function of mine: disable_mouse_for_a_second() { if xinput --disable "$1" 2> /dev/null then ( sleep 1s xinput --enable "$1" ) & return 0 else return 1 …
Vlastimil Burián
  • 3,024
  • 2
  • 31
  • 52
2
votes
1 answer

Bash function - second parameter in a function not taken

For some reason I cannot pass the 2nd parameter to a function which is on a another file, exactly here: $lsValidLocal | xargs -n 1 -I {} bash -c 'Push "{}" "**$inFolder**" The Push function on functions.sh does not read the 2nd parameter…
SimoneB
  • 158
  • 2
  • 12
2
votes
2 answers

Bash - Why $(sudo cat file) cannot find a file which exists?

Question Why $(sudo cat) cannot find a file which exists? This works: for host in $(cat /etc/ansible/hosts | cut -d ' ' -f 1 | grep -P '^master-' | sort | uniq) do ssh ${host} /bin/bash << EOF sudo cat…
mon
  • 18,789
  • 22
  • 112
  • 205
2
votes
1 answer

Dynamically change hostname in Bash PS1 prompt

I have a bash prompt that includes my hostname using either the \h variable (interpreted by PS1) or $(uname -n). Whichever one I use doesn't seem to make a difference. For software licensing reasons, I sometimes have to change my hostname, which is…
ardnew
  • 2,028
  • 20
  • 29
2
votes
1 answer

Why can't I use $(...) in PS1 instead of backticks?

My current PS1: PS1='\[\033]0;$TITLEPREFIX:${PWD//[^[:ascii:]]/?}\007\]\n\[\033[32m\]\u@\h \[\033[35m\]`date +%Y-%m-%d,%H:%M:%S` \[\033[33m\]\w\[\033[36m\]`__git_ps1`\[\033[0m\]\n$: ' Yes, it's a mess, but it serves me well - my prompts look…
Paul Hodges
  • 13,382
  • 1
  • 17
  • 36
2
votes
3 answers

Linux: run in subshell

I want to start a long-running process from a script that then exits, but I'm getting nowhere. Here's my most simple test. With each run, the console is blocked for 5 seconds. // Create script me@mine:~/workspace/bin$ cat > test.sh sleep 5 // Verify…
Steve11235
  • 2,849
  • 1
  • 17
  • 18