The Stack Overflow podcast is back! Listen to an interview with our new CEO.

Questions tagged [subshell]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0
votes
0answers
36 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
707 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
45 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
26 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
20 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
31 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
64 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
42 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 ...
0
votes
2answers
61 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 ...
5
votes
2answers
78 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
48 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 ...
10
votes
2answers
500 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 ...
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
54 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
41 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 ...
-2
votes
2answers
39 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
51 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
1answer
111 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
573 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://...
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
83 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
66 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 '...
9
votes
2answers
2k 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
2answers
62 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 $...
7
votes
1answer
891 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
129 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
72 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> <.....
2
votes
1answer
694 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 ...
2
votes
0answers
57 views

How to make a script being called inside another script in a ssh command? [closed]

I have a ssh calling a script in a server, named /opt/scripts/script1.sh. sshpass -p 'password' ssh user@ip /opt/scripts/script1.sh (I know sshpass is not secure but there's a reason for that in ...
1
vote
1answer
95 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
1answer
267 views

Run bash subshell with commands as other user and not return to parent shell

As part of my quest to preserve bashrc and vimrc when switching to non-root users, I plan on making some super ugly aliases to switch to other users, immediately execute some commands, and then drop ...
1
vote
1answer
543 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
239 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" >...
1
vote
2answers
3k views

How does (echo 'text' ; cat file.txt ) > new file.txt actually work? [duplicate]

So, here's a simple code: (echo "Some text to prepend"; cat gero.txt) > file.txt And I can't really grasp the mechanics of this code. So basically gero.txt is an already existing file, we create ...
1
vote
2answers
554 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 ...
51
votes
3answers
9k views

Is $() a subshell?

I understand the subshell syntax to be (<commands...>), is $() just a subshell that you can retrieve variable values from? Note: This applies to bash 4.4 based on different wording in their ...
0
votes
1answer
675 views

Setting bash flags in subshells - does it affect parent shell?

I have this in a bash script: ( set -o pipefail echo "foobar" | bash set +o pipefail ) do I need to reset pipefail setting, or can I just omit that line? In other words, does the pipefail ...
1
vote
2answers
93 views

Is it possible to findout if our command is sourced from a script or manually typed on command line?

We have an implementation to set environment for a particular tool with our custom command. Eg: custom_command tool_name This command logs the invokation time, user and tool_name in a log file. And ...
0
votes
1answer
379 views

grabbing exit code of background process/subshell

I am wondering if there is any difference between: long_running.sh & pid=$! and ( long_running.sh ) & pid=$! will the pid be capture consistently in both cases, or will there be some ...
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 ...
5
votes
1answer
1k views

Why (…) doesn't spawn a new child process when run in background?

After running the command { sleep 5; } & output of ps is (output 1) PID TTY TIME CMD 972 ttys000 0:00.27 -bash 2556 ttys000 0:00.00 -bash 2557 ttys000 0:00.00 ...
4
votes
1answer
1k views

Syntax error in nohup (…)

I am trying to test the nohup command in the bash shell by executing a command in the background and logging out and back in again and expecting the output of the command to be in the nohup.out file. ...
1
vote
1answer
258 views

Child process - listen for event when parent dies

I have a detached child process. It knows its parent's pid. Is there some way on both MacOS and Linux that I can listen for when the parent process exits, from the child process? I need to make a ...
1
vote
1answer
71 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 ...
2
votes
1answer
326 views

Run subshells in parallel and collect exit codes

I have the following, I thought it ran the subshells in parallel, but it actually seems to run them in series, and I cannot figure out why: #!/usr/bin/env bash set -e; set -m # allow for job control ...
1
vote
2answers
590 views

How to execute ant scripts in a differing directory to a shell script?

I'm writing a shell script to automate an elaborate build process. It includes execution of multiple ant scripts located in different directories. The shell script is in my home directory, while the ...
8
votes
1answer
7k views

Exporting a variable from inside a function equals to global export of that variable?

I use Ubuntu 16.04 with the native Bash on it. I'm not sure if executing #!/bin/bash myFunc() { export myVar="myVal" } myFunc equals in any sense, to just executing export myVar="myVal". Of ...
2
votes
1answer
310 views

BASH subprocess using SSH in script?

I want to do something like the following in a script $ ssh host1 "$(command)" However, the sub processes gets executed before the SSH connection is established because if I do the following $ ssh ...
3
votes
5answers
176 views

How to execute many files in the same directory in a minimal, DRY, pretty way?

I use Ubuntu 16.04 and I have a localization file, a file that executes many files, that I got after downloading my own GitHub project to my machine. This file contains a Bash script, and is named ...