Questions tagged [subshell]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0
votes
0answers
41 views

Bash completion not working in subshell

If I open a terminal window, or open a terminal in tmux, tab completion works as expected, but if I type bash to open a subshell, it doesn't. Below is my .bash_profile and .bashrc # .bash_profile [[ ...
0
votes
1answer
18 views

Are subshells run in parallel by default?

This question follows from this one and one of its answers' recommendation to read Linuxtopia - Chapter 20. Subshells. I'm a bit confused by this statement at the Linuxtopia site: subshells let ...
0
votes
0answers
38 views

How can I get exit status of subshell?

A classical example of piping: $ false | true $ echo ${PIPESTATUS[@]} 1 0 Same example run in a subshell and assigning results into a variable: $ process="$(false | true)" $ echo ${PIPESTATUS[@]} 0 ...
16
votes
3answers
3k views

What is the exact difference between a “subshell” and a “child process”?

According to this and this, a subshell is started by using parenthesis (…). ( echo "Hello" ) According to this, this and this, a process is forked when the command is started with a & echo "...
7
votes
3answers
3k 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 ...
0
votes
0answers
48 views

Terminate a subshell background process

I am starting entr as a subshelled background process as described in its man page to listen to changes to some file foo, occuring during interactive use of some program baz, and I would like to ...
10
votes
2answers
727 views

How to print and use a command output in a one-liner?

I'd like to pass the result of mktemp as argument to a command, let's say gcc -o. gcc -o $(mktemp) causes the result to be used, but I need to figure out the result. The only thing I could come up ...
2
votes
1answer
59 views

Nested command substitution does not stop a script on a failure even if -e and shopt -s inherit_errexit are set

Suppose you have a following script sandbox.sh. (This looks similar to Command substitution inside a function does not stop the script on a failure even if -e is set, but I believe a bit different ...
1
vote
0answers
29 views

How can I create a function that memoizes other commands?

I have created the following function to memoize any command: # zsh (( $+functions[ec] )) || { ec() print -r -- "$@" ecerr() { ec "$@" 1>&2 } ecdbg() ecerr "$@" gq() { ec "${(q+...
0
votes
0answers
21 views

Shell: Open REPL in function

I want to open an interactive REPL in my zsh functions (mainly for debugging purposes); Exactly like IPython.embed(). One way to do something similar is to simply invoke another instance of zsh, but ...
0
votes
1answer
35 views

Subshell Job Clarification

I run the command (Xorg & sleep 3; xeyes) & to test Xorg, and group it into a single subshell background job to make management easy. This works properly, and opens xeyes in the new Xorg ...
1
vote
1answer
73 views

Can you explain this inner working of this printf command in bash?

I stumbled onto the question Replacing only specific variables with envsubst and in one of the comments was this shell example: envsubst "$(printf '${%s} ' ${!PREFIX*})" < infile See the original ...
2
votes
0answers
52 views

Is job-control really meant to be supported in subshells and scripts?

Small premise I use subshells quite often to perform operations that involve changing the Shell Execution Environment, so as not to affect the main shell. I do it often from an interactive shell, and ...
5
votes
2answers
80 views

How to handle async subshell exit

Say I have this: set -e; ( docker stop notifier-server docker rm -f notifier-server exit 1 # explicitly exit with non-zero ) & wait; echo 'we are here now' will we always get to ...
0
votes
2answers
79 views

Set shell variable then invoke subprocess fails

I was playing with getopts, which has a shell variable OPTERR that influences its behavior. I wanted to change the value of OPTERR and invoke getopts on the same line, to affect the behavior of ...
1
vote
1answer
102 views

Export vars env to tmux's window/session/pane's env

Is there any way to export/upgrade list of variable env from a file or something to tmux's window/session/pane? Here I have to manually source .bashrc to update new var env to each tmux's pane.
0
votes
2answers
57 views

Precedence of subshells in relation to redirection?

Shell: GNU BASH I've been using the following command for a while now: echo "$(fmt -w 50 < foo.txt)" > foo.txt Without any issues. However, when I try: fmt -w 50 < foo.txt > foo.txt ...
11
votes
2answers
523 views

Why does a brace command group need spaces after the opening brace in POSIX Shell Grammar?

TL;DR: Why does POSIX brace group need spaces after { reserved word but subshell doesn't after reserved word (? POSIX shell grammar defines brace group and subshell as follows brace_group : ...
24
votes
3answers
4k views

How can I detect if I'm in a subshell?

I'm trying to write a function to replace the functionality of the exit builtin to prevent myself from exiting the terminal. I have attempted to use the SHLVL environment variable but it doesn't seem ...
10
votes
2answers
3k views

How can I get the pid of a subshell?

How can I get the pid of a subshell? For example: $ echo $$ 16808 This doesn't work, because the original shell expands $$: $ ( echo $$ ) 16808 Why does single quoting not work? After the ...
0
votes
0answers
53 views

Run script in background using `&` or `()&`?

If I want to run a script in background, for example a.sh, should I run ./a.sh & or should I put it in a subshell then background it? (./a.sh) & I test both and don't see difference, both ...
2
votes
1answer
74 views

Bash script to count number of executions instances does not work

I am writing a script that detects if there is any instance of it already running on my Linux machine and shows on screen the number of instances. The content of the "detect_itself.sh" script is: #!/...
1
vote
1answer
43 views

Is it possible for sendmail to not use all the output from a bash subshell piped to it?

( GNU bash, version 4.2.50(1)-release (powerpc-ibm-aix6.1.2.0)) ( echo "a" echo "b" echo "c" )| sendmail -F "Sender" recipients@a.com This runs inside a .sh file, and the log (when ran with the -x ...
6
votes
3answers
6k 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 ...
-2
votes
2answers
41 views

Identifying shell script's subshell number from within a script itself

I am accidentally caught in a desire to reveal the subshell number (BASH_SUBSHELL) from within the script itself, but i get subshell 0 Here is the script's line echo "Operated from subshell: $...
0
votes
1answer
66 views

Exported Date Variable not appearing in subshell

I'm currently working on creating 2 bashes with two different scripts which result in two files named with the same timestamp for easy comparison/processing later. The date variable gets created in ...
1
vote
2answers
1k views

cd in bash script without alias, function, source [duplicate]

I've written a bash script which should cd me to a directory. My problem is that only the directory in the subshell changes. I've read of many similar problems to this, but I want to know if there ...
1
vote
1answer
139 views

Terminating infinite-loop script embedded in tmux status bar

I have an infinite while loop bash script embedded in tmux status bar. In ~/.tmux/conf: set -g status-right '... #(path/loop.sh) ...' Tmux seems to run this script with sh -c. After tmux started: $ ...
3
votes
1answer
643 views

Process substitution inside a subshell to set a variable

I'm trying to run a script remotely and use its standard output to populate a variable. I'm doing this to avoid temporary files. Here's the pattern I'm trying: var=$(bash <(curl -fsSkL http://...
2
votes
1answer
1k views

Compare command output inside if statement without subshell

According to https://www.gnu.org/software/bash/manual/bashref.html#Command-Grouping Placing a list of commands between curly braces causes the list to be executed in the current shell context. But ...
1
vote
1answer
615 views

How to pass PID of one process to another process within the same shell script?

Okay, so this might be a very silly question and I don't write shell scripts too often. I'm trying to start 3 processes in the background, one after the another within a shell script, for example: #!/...
1
vote
2answers
632 views

Difference between pushd/popd and sub-shell+cd

I'm trying to understand if there are any benefits of using: pushd my_dir make all # ... or something else popd vs ( cd my_dir make all # ... or something else ) or is it merely a ...
22
votes
3answers
2k views

How does this script ensure that only one instance of itself is running?

On 19 Aug 2013, Randal L. Schwartz posted this shell script, which was intended to ensure, on Linux, "that only one instance of [the] script is running, without race conditions or having to clean up ...
0
votes
1answer
88 views

How can I stop a child process of a subshell (as per SIGSTOP) before the subshell exits?

In bash, when running ( sleep 123 &), the sleep 123 process will continue running, when the subshell exits. How can I stop the sleep 123 process before its parent subshell exits? I'm trying to ...
0
votes
1answer
68 views

Confused why script does not exit

I have this script verbatim: #!/usr/bin/env bash handle_json(){ while read line; do cat <<EOF {"@json-stdio":true,"value":{"mark":"$1","v":"$line"}} EOF done; } ( echo; echo; echo '...
1
vote
2answers
271 views

Background process of subshell strange behaviour

I wonder why some similar bash commands behave the way they do. I have a bash script foo: #!/usr/bin/env bash while true do echo "reading" read data echo $data echo "stderr msg" >...
3
votes
2answers
822 views

CTRL C behavior in hierarchy of shells

I open a shell (like sbt or node debug from my bash). Then inside this shell, I open another one (using scala or node repl respectively). Now I want to close the last shell and come back to the first ...
1
vote
1answer
77 views

Are processes run from Bash run in a “sub shell”?

When I run an executable (like a.out) from a Bash shell, is that executable run in some kind of "sub" shell, i.e. different from the shell at which I'm typing? I'll try illustrate my question with an ...
30
votes
6answers
23k views

exit shell script from a subshell

Consider this snippet: stop () { echo "${1}" 1>&2 exit 1 } func () { if false; then echo "foo" else stop "something went wrong" fi } Normally when func is ...
4
votes
2answers
927 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: ...
0
votes
1answer
221 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 ...
0
votes
3answers
146 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....
0
votes
2answers
71 views

“Globally Store” PID of file started from bash script

I'm compiling a latex file in a script and then I'm trying to either open a new pdf reader window or reload the current one (llpp being my reader), so after compiling my script does pdflatex $...
8
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 ...
7
votes
1answer
934 views

exit not terminating the script [duplicate]

exit doesn't terminate the script when error is called.. output Error: Could not resolve localhost after exit script #!/bin/sh resolve_ip (){ if [ -z "$1" ]; then host="localhost" ...
1
vote
1answer
159 views

Weird behavior in zsh random generator

I have this script to generate $1 digit(s) of mixed random char from the defined charlists as arrays. #!/bin/bash charlist1=(a b c d e f g h i j k l m n o p q r s t u v w x y z) charlist2=(0 1 2 3 4 ...
0
votes
1answer
78 views

Catch result of sub-process in Z-shell

AAA <options>; CCC AAA <options> AAA, CCC are different commands. Here we're in an zsh script - for example: vi script.zsh <...> AAA <options>; CCC AAA <options> <.....
1
vote
1answer
209 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
2answers
719 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 $ ...
2
votes
1answer
787 views

VNC clipboard not working

If I uncomment the two lines in my ~/.vnc/xstartup that enable 'normal' desktop environment for VNC, the vncconfig -iconic & command is ignored: #!/bin/sh # Uncomment the following two lines for ...