Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [subshell]

The tag has no usage guidance.

20
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 ...
-1
votes
0answers
62 views

bash: find exec in a subshell with multiple commands

I am using find and then executing a set of commands in a subshell as follows: find ${CWD} -name "Sourced.sh" -exec bash -c '\ i="$1"; echo ${i}; \ cat ${i} > ${i}.temp; \ "${dScriptP}"/Substitute....
0
votes
1answer
64 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
55 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 '...
4
votes
2answers
591 views

How can I get the pid of a subshell? [duplicate]

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
42 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
493 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" ...
0
votes
0answers
43 views

Unexpected value of $? when using subshell [duplicate]

I want to check if a file exists in HDFS or not and capture it in the variable SUCCESS_FILE_CHECK, i.e., if file exists the SUCCESS_FILE_CHECK should be 0 else it can be any other value. $ ...
1
vote
1answer
57 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
49 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
97 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
45 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
0answers
17 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
75 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
210 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
80 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" >...
0
votes
0answers
41 views

Opening an application without blocking next command

Looking for a way to open an application such that my script will continue running and not wait for the closure of an application. Attempts: vmd : Waits for closure of application to proceed to next ...
0
votes
0answers
65 views

Environment variables in Subshell

I try to execute ImageMagick on MacOS in an Ant script. Although there's no problem while running in the Terminal, Ant throws an error... [exec] dyld: Library not loaded: /ImageMagick-7.0.7/lib/...
1
vote
2answers
751 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 ...
0
votes
2answers
218 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 ...
46
votes
3answers
6k 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
348 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
89 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
132 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 ...
3
votes
1answer
438 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 ...
4
votes
1answer
449 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
708 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
197 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
61 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
147 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
357 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 ...
3
votes
1answer
3k 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
170 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
163 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 ...
7
votes
2answers
665 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 "...
2
votes
1answer
358 views

Variable inherited in the subshell without exporting in the main shell [duplicate]

I am having the below shell script var="this is a test" ls -ltr| while read file do echo $var done echo $var I am getting the below output: this is a test this is a test this is a test How ...
2
votes
3answers
891 views

Silently kill subshell?

I want to implement something like this Q/A but for a sub-shell. Here is a minimal example of what I'm trying: (subshell=$BASHPID (kill $subshell & wait $subshell 2>/dev/null) & sleep ...
8
votes
1answer
391 views

Why does exiting with a stored returncode of a nested command result in different returncodes in Dash and Bash?

Running bash -c 'bash -c "echo test1; exit 1;" &> /tmp/x; buildresult=$?; tail -n 100 /tmp/x; exit $buildresult;' results in test1 being printed to console and echo $? to print 1 which in my ...
1
vote
2answers
79 views

How to get output of variable with line breaks?

I want to get onre output of top to variable for future processing. I tried: top=$(top -n1) top=$(top -n1 -w80) top=$(top -n1 -b -w80) top=$(COLUMNS=100 top -b-n1) When I run echo $top then I see ...
6
votes
1answer
494 views

Why is the output on this ping outside the subshell?

I have a simple script that checks if an interface is connected: connected=$(ping -I $1 -qc 1 8.8.8.8 \ && echo "connected" \ || echo "not connected") echo "$connected" | tail -n1 I'm ...
5
votes
2answers
410 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 $ ...
0
votes
1answer
234 views

can't capture PID from background process started in a sub-shell that's running in a heredoc passed to `sudo su`

I'm trying to capture the PID of a process started in a sub-shell so I can check if the process started correctly, the process in question is a puma app. sudo su - $APPUSER <<-BASH PID="\$(...
4
votes
1answer
441 views

Difference between subshells and process substitution

In bash, I want to assign my current working directory to a variable. Using a subshell, I can do this. var=$(pwd) echo $var /home/user.name If I use process substitution like so: var=<(pwd) ...
1
vote
2answers
2k 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 ...
1
vote
0answers
918 views

Restoring tty correctly with stty

I'm writing a bash script to read some user input (like a password request), and I'd like to capture the result and pass it through its stdout. (I'm working in a GNU/Linux environment, with recent ...
1
vote
1answer
276 views

“. ~/.bashrc” does not set variables as expected in the calling script

Background I have written a script to install node via nvm. Issue The command . $LOCAL_SHELL_CONFIG_FILE inside install_latest_stable_node() does not run and thus, when it prints the PATHs for node ...
6
votes
1answer
2k views

Flock doesn't seem to be working

I've recently been trying to create a shell script for a minor project, and for some reason, the flock command isn't working for me properly. Whenever I invoke it in a subshell the atomic way and put ...
5
votes
2answers
548 views

Is there a simple test for anything printed on stderr in shell/Bash?

I'd like invoke a command inside a shell script for Continuous Integration purposes. Exit status 0 means succes, otherwise failure. I'm writing a wrapper script to run several commands and fail if any ...
0
votes
0answers
15 views

Variable will not stay set after being modified inside of a PIPE while loop [duplicate]

I was writing a script, and ran into a problem where my variable will not stay set when I modify it from inside of a | while loop. To illustrate, I wrote this example: #!/bin/bash thevar="Foo" ...
0
votes
1answer
342 views

Subshell inside quotes

I'm writing a small shell script like this: curl -X POST --header 'Bearer "$(printf user:pass | base64)"' 'https://api.com/v1/auth' To debug I switched to echo: echo 'Bearer "$(printf remote-key-...