DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

Aubrey Portwood
Aubrey Portwood

Posted on • Updated on

How to run commands in a sub-shell and suppress output in the background that's ZSH & shellcheck compatible

TLDR: This works:

( (
    bar
    foo

) 1>&- 2>&- & )
Enter fullscreen mode Exit fullscreen mode

So, basically, what this does is run a sub-shell then bury your commands in another sub-shell and closes all STDOUT being reported and is sent to the background. I honestly am still not sure how this works!


&!

Because I use shellcheck with my zsh scripts the following flagged an unknown error in shellcheck (because shellcheck does not support zsh at the moment) and it wouldn't lint the file:

() (

    foo
    bar

) &> /dev/null &!
Enter fullscreen mode Exit fullscreen mode

Specifically it was the &! that wasn't working (but is a valid ZSH directive to suppress background output). I would always get job output at the least using most of the suggestions e.g. on Stack Exchange. So, I went down a Googling-rabbit-hole with no answers, until I landed on:

โ†’ https://www.baeldung.com/linux/run-multiple-commands-in-background

Which I then had to grok the horrible formatting and came up with the above that is Bash compatible to run commands in the background with no output!

๐Ÿ™Œ

Top comments (3)

Collapse
 
aubreypwd profile image
Aubrey Portwood

Another update to use ) 1>&- 2>&- & ) today, as I still was getting output! See cyberciti.biz/faq/how-to-redirect-... on why I chose this. Really it's because it seems to just be working :)

Collapse
 
aubreypwd profile image
Aubrey Portwood

Updated to use ) > /dev/null & ) since I found output being present when using ) & )

Collapse
 
sso profile image
Sall

Nice post, I liked the idea. If you would like to collaborate on similar topics and matterialize some ideas, you can find us here:
๐Ÿช„ wiki.zshell.dev ๐Ÿง™โ€โ€โ™€๏ธ

Stop sifting through your feed.

Find the content you want to see.

Change your feed algorithm by adjusting your experience level and give weights to the tags you follow.