1

I have a command which takes two arguments. Assume we want to create both arguments by process substitution:

cat <(var=123; echo $var) <(echo $var)

What has to be done, to make $var available in the second substitution?

  • 1
    The problem is that you are only assigning var in your first subshell; var is thus only available within its scope. You might want something like var=123; cat <(echo $var) <(echo $var) – DopeGhoti Feb 13 '16 at 0:28
  • Okay, this makes it readable, but not writable. I need to alter $var. – awado Feb 13 '16 at 0:31
  • 1
    var=123; PARAM1=$(var=456 echo $var); PARAM2=$var; cat <(echo $PARAM1) <(echo $PARAM2) is about as close as I cam come to what it looks like you're asking for. – DopeGhoti Feb 13 '16 at 0:35
  • What has to be done? A shell rewrite that'll use threads instead of forks. That's probably not going to happen anytime soon. – PSkocik Feb 13 '16 at 0:53
  • DopeGhoti's second comment did the trick. Thanks a lot for that, @DopeGhoti! My initial goal is to color the differences of a command output over time. For example from iotop. I cannot do it via writing its output to tmpfs, due to timing issues. It gets me too many false positives. – awado Feb 13 '16 at 1:04
1

The problem is that you are only assigning var in the first subshell; the second subshell doesn't have access to anything assigned therein. To access a variable in both, you need to declare it beforehand:

var=123
cat <(echo $var) <(echo $var)

If you need to keep the original and track things that change it:

var=123                               # set the default value
PARAM1=$(/bin/something; echo $var)   # something could edit var's value
PARAM2=$(/bin/otherthing; echo $var)  # so could otherthing
cat <(echo $PARAM1) <(echo $PARAM2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.