subshell

bash - pipe creates a subshell

三世轮回 提交于 2021-02-18 06:44:07
问题 So this read is executed after the pipeline, which means that the output of the echo gets read into str - but because it is after a pipe, the contents of str are now in a subshell that cannot be read by the parent shell. My questions is - what happens in to the contents of str ? Does the pipe create a subshell, and then once the content are read into str , does the parent process kill the child process and str is erased - or does the contents of str live on somewhere outside the shell. Like

Replacing 'source file' with its content, and expanding variables, in bash

﹥>﹥吖頭↗ 提交于 2020-12-08 07:55:53
问题 In a script.sh, source a.sh source b.sh CMD1 CMD2 CMD3 how can I replace the source *.sh with their content (without executing the commands)? I would like to see what the bash interpreter executes after sourcing the files and expanding all variables. I know I can use set -n -v or run bash -n -v script.sh 2>output.sh , but that would not replace the source commands (and even less if a.sh or b.sh contain variables). I thought of using a subshell, but that still doesn't expand the source lines.

Replacing 'source file' with its content, and expanding variables, in bash

点点圈 提交于 2020-12-08 07:55:09
问题 In a script.sh, source a.sh source b.sh CMD1 CMD2 CMD3 how can I replace the source *.sh with their content (without executing the commands)? I would like to see what the bash interpreter executes after sourcing the files and expanding all variables. I know I can use set -n -v or run bash -n -v script.sh 2>output.sh , but that would not replace the source commands (and even less if a.sh or b.sh contain variables). I thought of using a subshell, but that still doesn't expand the source lines.

Why can't I use $(…) in PS1 instead of backticks?

℡╲_俬逩灬. 提交于 2020-12-06 11:45:25
问题 My current PS1: PS1='\[\033]0;$TITLEPREFIX:${PWD//[^[:ascii:]]/?}\007\]\n\[\033[32m\]\u@\h \[\033[35m\]`date +%Y-%m-%d,%H:%M:%S` \[\033[33m\]\w\[\033[36m\]`__git_ps1`\[\033[0m\]\n$: ' Yes, it's a mess, but it serves me well - my prompts look like this: P2759474@RVPTINTCL415MQC 2017-10-06,11:20:18 ~/repos/jdk (master) They are even color coded, with user@machine in green, timestamp in purple, current location in yellow, and any git branch in blue. I'm just a little annoyed that I have to use

Bash 'swallowing' sub-shell children process when executing a single command

人走茶凉 提交于 2020-07-15 07:01:51
问题 Bumped into an unexpected bash / sh behavior and I wonder someone can explain the rationale behind it, and provide a solution to the question below. In an interactive bash shell session, I execute: $ bash -c 'sleep 10 && echo' With ps on Linux it looks like this: \_ -bash \_ bash -c sleep 10 && echo \_ sleep 10 The process tree is what I would expect: My interactive bash shell process ( $ ) A children shell process ( bash -c ... ) a sleep children process However, if the command portion of my

zenity --auto-kill: Killing a subshell does not kill its child processes?

北城余情 提交于 2020-06-29 13:19:25
问题 I am having a really hard time understanding the behaviour of zenity --progress --auto-kill . It appears to not kill its parent process' subprocesses but to detach them somehow. Consider the following shell script long-live-the-subshell.sh : #!/bin/sh ( echo sleeping...>&2; sleep 5; echo woke up >&2 ) | zenity --progress --auto-close --auto-kill To reproduce the mentioned behaviour: execute the script sh long-live-the-subshell.sh click Cancel on the progress bar wait another 5 seconds see

How do I pass subshell results (array) to an SSH command?

三世轮回 提交于 2020-01-23 17:15:25
问题 Trying it this way: #!/bin/bash myvals=`psql -d mydb -c "select id from table1 where 't'"` ssh user1@host1.domain.tld "for i in $myvals; do echo \$i >> values; done" As long as psql returns just one value, it works fine. But if its several values, I receive this response: bash: -c: line 1: syntax error near unexpected token `2' bash: -c: line 1: `2' Also, I tried to: myvals='1 2 3' And then it works fine: the values 1 2 3 are appended to the "values" file on the remote host; no error mesages.

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

我的未来我决定 提交于 2020-01-04 15:13:33
问题 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 the alias, cd $directory . This is great and it seems to be working as expected. What I'm looking for is for when the subshell is launched, the cd happens automatically, so I tried: ( bash | cd ) thinking it would launch the subshell and cd to the

In bash how do I exit a script from a function that is piped by tee?

╄→гoц情女王★ 提交于 2020-01-03 15:33:07
问题 I'm trying to understand why whenever I'm using function 2>&1 | tee -a $LOG tee creates a subshell in function that can't be exited by simple exit 1 (and if I'm not using tee it works fine). Below the example: #!/bin/bash LOG=/root/log.log function first() { echo "Function 1 - I WANT to see this." exit 1 } function second() { echo "Function 2 - I DON'T WANT to see this." exit 1 } first 2>&1 | tee -a $LOG second 2>&1 | tee -a $LOG Output: [root@linuxbox ~]# ./1.sh Function 1 - I WANT to see