The tag has no wiki summary.

learn more… | top users | synonyms

5
votes
3answers
173 views

Use bash's read builtin without a while loop

I'm used to bash's builtin read function in while loops, e.g.: echo "0 1 1 1 1 2 2 3" |\ while read A B; do echo $A + $B | bc; done I've been working on some make project, and ...
7
votes
2answers
191 views

Why is a variable visible in a subshell?

The Learning Bash Book mentions that a subshell will inherit only environment variables and file descriptors, etc., and that it will not inherit variables that are not exported: $ var=15 $ (echo ...
2
votes
2answers
537 views

Assign Subshell background process pid to variable

I am currently starting a background process within a subshell and was wondering how can I assign its pid number to a variable outside the subshell scope? I have tried many different ways but MYPID ...
2
votes
1answer
50 views

Bash - how to make explicit operator precedence without creating a subshell

I'm sure this is posted somewhere, but I haven't been able to find it. In Bash, how does one specify operator precedence (aka command grouping) without creating a subshell? In most other languages ...
2
votes
0answers
41 views

Bash tab completion fails inside of command substitution

I've been using kill $(pidof ) to terminate programs for a while, and since last night, every time I try to use tab completion inside the $() it throws an error, bash: unexpected EOF while looking ...
2
votes
1answer
47 views

Change the PS1 prompt in a subshell sh called from parent bash

When I am running an interactive bash session, I sometimes experiment with sh. I call sh directly from this bash session. My question is, how can I change the PS1 prompt of this subshell? I would ...
4
votes
1answer
122 views

How to make a variable from a subshell available in the parent shell

I've written a quick-and-dirty script to time some reports from a web service: BASE_URL='http://example.com/json/webservice/' FIRST=1 FINAL=10000 for report_code in $(seq 1 $FINAL); do (time -p ...
6
votes
2answers
468 views

Do parentheses really put the command in a subshell?

From what I've read, putting a command in parentheses should run it in a subshell, similar to running a script. If this is true, how does it see the variable x if x isn't exported? x=1 Running ...
1
vote
1answer
194 views

Understanding behavior of subshell and stdout with pipe

Let us say I have .c file and corresponding executable (let us say foo) that takes in some input from stdin and then makes a sys call to /bin/sh. Feeding input to this executable like following: ...
2
votes
3answers
143 views

CTRL C behavior in hierarchy of shells

First of all sorry about terminology errors, I'd try to be clear. 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 ...
10
votes
3answers
2k views

Bash subshell creation with curly braces

According to this, placing a list of commands between curly braces causes the list to be executed in the current shell context. No subshell is created. Using ps to see this in action This is the ...
13
votes
5answers
530 views

Stateful bash function

I’d like to implement a function in Bash which increases (and returns) a count with every call. Unfortunately this seems non-trivial since I’m invoking the function inside a subshell and it ...
1
vote
1answer
322 views

How to trace system calls being invoked after typing command?

This question is relevant to the question of 'what happens when we type a simple command on shell?' I posted earlier. (I thought it would be better to post it seperately. But if not, please let me ...
16
votes
2answers
1k views

What happens when we type a simple command on shell? [duplicate]

I have a simple doubt on execution of a simple command. As per my understanding, when we type a command such as ls on interactive shell, Shell interpretes the command. Shell creates a child process ...
0
votes
1answer
241 views

How to pass a flag from one script to a function in another script

I have a script that installs a bunch of programs (homebrew, git, etc) and I have it log to stdout all the things that its doing along the way, but I want the ability to pass a flag, such as --no-log ...
2
votes
1answer
292 views

When should you use subshells vs `xargs`?

Many simple commands using xargs could be rewritten as commands using subshells. For instance, here is something I was using earlier today, to concatenate the ten largest binaries in /usr/bin, written ...
-3
votes
1answer
89 views

why does the dollar sign cause this command to be executed?

test_script.sh: #!/bin/sh D1="$(dirname $0)" echo $D1 D0="(dirname $0)" echo $D0 What I type on the command line: $ ./test_script.sh Output: . (dirname ./test_script.sh) What significance ...
2
votes
2answers
344 views

BASH and Subshells

From what I gather from the online docs, the following should spawn a subshell for the part of the command embedded with {}: $ bash -c '{ sleep 10; echo "Sleeping process", $$; } & echo $$; ' ...
6
votes
2answers
267 views

Expand subshell before executing it?

I often run commands using subshells, and sometimes would like to have the subshells expanded before I run something.. This way I could verify what I'm doing, and possibly edit what's about to happen ...
2
votes
1answer
512 views

How to get functions propagated to subshell?

Solaris / sh I have a few functions defined in a file which gets loaded via . ./some_file.sh When I start a subshell with sh All my function definitions are lost but when I do env I do ...
3
votes
2answers
318 views

$BASHPID And $$ differ in some cases

I'm reading "BASH pocket guide of Oreilly". It said: The process ID of the current Bash process. In some cases, this can differ from $$. Above explanation , explained $BASHPID variable. ...
2
votes
1answer
154 views

How to set Cols and Lines for a Subprocess

I have a script that runs a series of scripts numbered 001,002,003,004... etc down to 041 right now, will be more in the future - and these scripts them selves use some cursor control to print a ...
1
vote
1answer
364 views

curly braces and subshell

I can't figure it out. As I read in documentaion, {} doesn't create a subshell. However, looks like that sometimes it does: $ unset T; echo "T_bfr=$T"; echo $$; { echo $$; export T=1; }; echo ...
7
votes
4answers
3k 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 ...
5
votes
2answers
1k views

Behavior of stdin/stdout in conjunction with subshells and cd command

I have the following series of commands: cd / && ls | ( cd /tmp && cat >dumpfile) This series of commands does the following: it creates a file named /tmp/dumpfile. This file ...
14
votes
1answer
926 views

In zsh, difference between cat <(cat) vs cat | cat vs cat =(cat)?

As a zsh/process substitution noob, I expected cat <(cat) and cat | cat to do the same thing: copy lines from stdin to stdout. My understanding was that both would execute a cat in a subshell, ...
14
votes
2answers
3k views

Rule for invoking subshell in Bash?

I seem to misunderstand the Bash rule for creating a subshell. I thought parentheses always creates a subshell, which runs as its own process. However, this doesn't seem to be the case. In Code ...
4
votes
3answers
4k views

How to get subshell's PID in Korn Shell (equivalent of $BASHPID)

In bash you have this handy variable: $BASHPID wich always returns the currently running subshell's PID. How can I get a subshell's PID in ksh? For example see the code below: #!/usr/bin/ksh93 echo ...
12
votes
2answers
603 views

Bash syntax highlighting of subshells

Considering the large amount of questions which turn out to be related to subshells in Bash ("Why doesn't my variable increment in this piped while read loop?"), I just thought it would be very nice ...