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 to refer to some editor or service where code which will be executed in subshells is formatted differently from the code executed in the parent shell. It might be useful as an educational device (see the difference between code | code and code < <(code) instantly). Does this exist?

Bonus points if anyone implements it for jEdit or VIm.

Obviously it doesn't have to be perfect (no syntax highlighting is, in my experience), but I suspect things like (foo=bar; echo $foo;) and command | while read ... shouldn't be too difficult for a start.

  • I'm sure you could add it to vim's code highlighting. – Kevin Feb 15 '12 at 17:05
  • 2
    @Kevin: The question is rather whether it already exists, in any form. It could of course be added to any editor which supports a Turing-complete highlighting system. – l0b0 Feb 16 '12 at 9:36
  • 1
    In the case of the piped while loop, POSIX doesn't specify which part of the pipe executes in the parent, so as a teaching tool, this highlighting, if it existed, would teach people to write non-portable shell scripts :( – James Youngman Feb 26 '12 at 23:44
  • @JamesYoungman: Another reason to make it Bash-specific :) – l0b0 Aug 21 '12 at 13:06

I don't think this exists. It would be useful, but hard to implement by standard means of syntax parsing used in editors. From the parsing point of view, there are many keywords and special symbols that would have to be analysed to determine a block of code that belongs to a subshell.

But I'd be happy to see I'm wrong and someone has put the effort needed to create such configurations.


My Vim already does this.

It actually bothers me, because it causes comments inside of the subshells not to get the proper highlight applied.

enter image description here

This is Vim 7.3 patch 874 compiled from source.

  • Sometimes I set the filetype to zsh when I'm working within a subshell. That corrects the syntax highlighting. – user26112 Jul 15 '13 at 23:42
  • OMG that is perfect. Thanks!! I have to have this entire huge block (with many comments) inside the subshell so that I can do the set -x; set -e on that section. And to capture all of that output including stderr to a log. – Steven Lu Jul 15 '13 at 23:43
  • 1
    I don't see any formatting in the subshell, which doesn't seem very useful. Or is it just missing from the screenshot? – l0b0 Jul 16 '13 at 7:09
  • the variable set and trap keyword are highlighted there. it just failed to highlight comments. the zsh file type works though. Now, as far as differentiating the sub shell from the main shell, (which after all is what you wanted I guess), well....... use some comments? – Steven Lu Jul 16 '13 at 13:00

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.