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

Using awk to parse a config file

I'm trying to make an awk command which stores an entire config file as variables. The config file is in the following form (keys never have spaces, but values may): key=value key2=value two And my awk command is: $(awk -F= '{printf "declare…
Addison
  • 7,322
  • 2
  • 39
  • 55
2
votes
1 answer

Proper way to store the return value of a piped function in bash

I have an array of functions which I call inside a loop in bash. Whenever one of these functions returns an error, I keep track of it storing that function's name inside an error array to show to the user. This is the current working…
dvilela
  • 1,200
  • 12
  • 29
2
votes
2 answers

Shell script split a string by space

The bash shell script can split a given string by space into a 1D array. str="a b c d e" arr=($str) # arr[0] is a, arr[1] is b, etc. arr is now an array, but what is the magic behind? But, what exactly happened when we can arr=($str)? My…
Jes
  • 2,614
  • 4
  • 25
  • 45
2
votes
1 answer

Bash while loop stops after first iteration when subshell is called

This contrived bash script demonstrates the issue. #!/bin/bash while read -r node ; do echo checking $node for Agent; PID=$(ssh $node ""ps -edf | grep [j]ava | awk '{print $2}'"") echo $PID got to here. done <…
sparco1500
  • 187
  • 3
  • 15
2
votes
2 answers

Feeding sed matched block through a command

I have json lines that contain multiple parts per line that look like this: "SomeDate":"Date(-2156284800000)", I would like to convert each occurrence in all lines into something more human readable: "SomeDate":"1901-09-03 00:19:32", I tried using…
Niels Basjes
  • 10,424
  • 9
  • 50
  • 66
2
votes
2 answers

$> bash script.sh ... does the forked bash process in turn create a sub-shell?

If I run: $> bash script.sh a fork-and-exec happens to run the bash binary. Does that process execute script.sh or does it create a sub-shell in turn in the same way that $> ./script.sh first creates a sub-shell to execute the script?
euphoria83
  • 14,768
  • 17
  • 63
  • 73
2
votes
1 answer

Supervise a Python subshell

I want to run a Python Interpreter as an inferior process in bash or zsh. During this time, I would like to send commands to the process and see the output in STDOUT. Something like this: $ in=/dev/shm/python_test_in $ out=/dev/shm/python_test_out $…
PythonNut
  • 6,182
  • 1
  • 25
  • 41
2
votes
1 answer

Bash: command grouping with curly braces inside subshell

When I'm trying something like this: ( echo && { echo ; echo } ) I'm getting: -bash: syntax error near unexpected token `)' I'm sure that I need to use subshell. And I'm sure that I need grouping inside of it. So how do I avoid the syntax…
2
votes
1 answer

Exit status of tee>(...)

I am executing: Command1 | tee >(grep sth) || Command2 I want Command2 to be executed based on the exit status of grep, while in the current configuration it is being executed based on the result of tee. As far as I know pipefail and pipestatus…
Aman
  • 1,157
  • 7
  • 13
2
votes
1 answer

make -e only exit from subshell

#!/bin/bash -eu items="1 2" for item in $items; do ( echo one false echo two ) ||: done I wish false line to break a subshell, but continue processing the outer loop. I.e. the expected output is one one however, I…
Ixanezis
  • 1,631
  • 13
  • 20
2
votes
1 answer

In a subshell, how to read an environment variable from the calling shell?

For the obvious reason I do not want to export COLUMNS in my normal bash shell. However for the purpose of one particular subshell, I need access to the value of COLUMNS of its parent. In other words, in a regular bash shell, I want to call a shell…
asoundmove
  • 1,292
  • 3
  • 14
  • 28
2
votes
3 answers

error message when using loop in shell command

What I'm doing wrong? unset list list=("A" "B" "C") /bin/sh -c "for i in `seq 1 ${#list[@]}` ; do echo $i ; done " it should return: 1 2 3 instead of: /bin/sh: -c: line 1: syntax error near unexpected token `2' /bin/sh: -c: line 1: `2'
Julio Fong
  • 497
  • 3
  • 5
  • 14
2
votes
3 answers

run a subprocess from ruby without waiting for it to return

Possible Duplicate: Spawn a background process in Ruby Spent a couple days poking at this. I was using ruby 1.8.7 from the OS until recently. I would call a subshell with backticks. The subshell was a bash wrapper that would call run any…
mike
  • 326
  • 1
  • 5
  • 14
2
votes
1 answer

Subshell in a function cannot find basic bash commands when called from another function

I have the following bash script (this is a simplified version of a more complex script). #!/usr/bin/env bash set -x function execute() { `$1` # same as $($1), gives "command not found" as do all the following: # $1 # or ${1} # eval…
Potherca
  • 13,207
  • 5
  • 76
  • 94
1
vote
1 answer

How to list all the linux aliases

I am aware that in Linux I can use the alias command to get a list of defined aliases. I am now trying to do the same through Go code with: func ListAlias() error { out, err := exec.Command("alias").Output() if err != nil { …
raviabhiram
  • 671
  • 2
  • 8
  • 21