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

Alias to 'cd' Command with Subshell Not Working as Expected

I just learned about aliases in bash. I created one like so: alias="cd $directory" where $directory is from use input. In another shell script, I can launch a subshell like so: ( bash ) which brings me to the subshell, where, if I run cd, I go to…
nicorellius
  • 3,715
  • 4
  • 48
  • 79
2
votes
4 answers

Why doesn't this bash code work?

x="a=b" `echo $x` echo $a I expect the second line to generate "a=b", and execute it in the context of the main shell, resulting in a new variable a with value b. However, what I really get (if I enter the commands manually) is the error message…
user500944
2
votes
4 answers

String expansion in bash with embedded quotes

Problem: The following shell script code does not produce the expected result: # MYSQL, MyUSER MyHost etc ... all defined above as normal TARG_DB="zztest"; DB_CREATE="$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse 'create database…
dreftymac
  • 31,404
  • 26
  • 119
  • 182
2
votes
1 answer

Why does recursive function go though only one sub directory

I have found the answer to get my code to work but I want to know why it works and my code doesn't rFunc() { for d in *; do if [ -d "$d" ]; then cd "$d" rFunc fi #Do Something done } This code will go though only one sub…
Gary
  • 318
  • 3
  • 14
2
votes
2 answers

How does the behavior of a function change if it is within a subshell?

When making functions within a script it bash, it appears that people often have the function run within a subshell, ie function(){( )} instead of function(){ } What are the benefit/downsides of using {()} rather than just {} if any?
Jengels
  • 440
  • 6
  • 15
2
votes
0 answers

A `cd` command in an executed Powershell script still affects the current session

Coming from the Unix world, scripts when executed (as opposed to sourced) are run in a subshell, and thus variable declarations and cd commands do not affect the actual current session (the one where we type our commands). For reference: # Executing…
adamency
  • 682
  • 6
  • 13
2
votes
1 answer

Bash subshell to file

I'm looping over a large file, on each line I'm running some commands, when they finish I want the entire output to be appended to a file. Since there's nothing stopping me from running multiple commands at once, I tried to run this in the…
Moshe
  • 4,635
  • 6
  • 32
  • 57
2
votes
2 answers

POSIX/Bash pad variable with trailing newlines

I have a variable with some lines in it and I would like to pad it with a number of newlines defined in another variable. However it seems that the subshell may be stripping the trailing newlines. I cannot just use '\n' with echo -e as the lines may…
scorch855
  • 302
  • 1
  • 9
2
votes
0 answers

Why is my bash "history" command 56x slower in a subshell?

Why does running the bash history builtin function in a subshell run 56x slower? (I have 22k lines in my history. Maybe that is significant?) $ time history > /dev/null real 0m0.064s user 0m0.049s sys 0m0.015s $ # in a subshell it runs…
Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
2
votes
1 answer

Single statement subshell doesn't seem to inherit ERR trap when using "set -o errtrace"

Consider this minimal example, which I run as an executable. #!/bin/bash set -E trap 'echo "ERR trap triggered"' ERR ( echo "hello" >/dev/null ls /root/ ) Notice the two ERR trap messages in the following output: ls: cannot open directory…
jmrah
  • 5,715
  • 3
  • 30
  • 37
2
votes
1 answer

Why basename with a suffix doesn't work in a subshell when used with find?

The following command doesn't do the subtitution, why? find ./ -name "*.dng" -exec echo `basename \{\} .dng` \; but this command work: find ./ -name "*.dng" -exec basename \{\} .dng \; What I'm actually trying to do is to find all the dng in my…
Fractale
  • 1,503
  • 3
  • 19
  • 34
2
votes
3 answers

How can I make bash 'pop' and 'shift' functions that actually return the item popped or shifted?

Running $ echo $BASH_VERSION 4.3.42(1)-release given these two functions: ashift () { declare -n arr;arr="$1" ((${#arr[@]} == 0)) && return echo "${arr[0]"} arr=("${arr[@]:1}") } apop () { declare -n arr="$1";shift …
mpersico
  • 766
  • 7
  • 19
2
votes
0 answers

How to prevent syntax errors when reading BASH associative array values which contain slashes from a child process?

I'm using bash 4.4.19(1)-release. At the start of my program I read customer configuration values from the command line, configuration file(s), and the environment (in decreasing order of precedence). I validate these configuration values against…
Alex Jansen
  • 1,455
  • 5
  • 18
  • 34
2
votes
1 answer

How shell executes this code 'cd $dir && php -f test_loop.php &'

I wonder how shell executes this code in .sh file(like test.sh): while true do # check php process. If not running, run it. cd $some_dir && php -f some_file.php & # sleep 1s done Some output: ps -ef|grep test|grep -v grep root …
Max
  • 547
  • 4
  • 9
2
votes
2 answers

where is documentation of < when it spawns a subshell

Apparently < and | behave similarly. cat file | wc wc < file Is there a name for this '<' symbol when used in this way, so that one could find its documentation? man < man '<' do not reveal anything; and in a Web search, the symbol is ambiguous,…
Jacob Wegelin
  • 1,304
  • 11
  • 16