Every time starting the machine, I run the following program:

$ cat start.sh
#! /bin/bash
google-chrome &> /dev/null &
lantern &> /dev/null &
xdg-open . &> /dev/null &
emacs &> /dev/null  &
code ~/Programs/ &> /dev/null &
xdg-open ~/Reference/topic_regex.md &> /dev/null &

Cumbersome &> /dev/null &... How could I shorten the logic?

  • 2
    You can put all or most of those programs in your Startup Applications. May 21 '19 at 21:52
  • great, fantastic application. Errors usually occurred to gnome-session causing abrupt crashing, so I start them from tmux server. @DennisWilliamson
    – AbstProcDo
    May 21 '19 at 23:46

The part &> /dev/null means output redirection. You can redirect multiple commands to the same file by grouping them into a block:

#! /bin/bash
google-chrome &
lantern &
xdg-open . &
emacs  &
code ~/Programs/ &
xdg-open ~/Reference/topic_regex.md &
} &> /dev/null

The same thing, however, cannot be used to start individual commands in the background (&). Putting & after the block would mean running the whole block as a single script in the background.

  • Do note that the &> syntax is deprecated and should be replaced with >/dev/null 2>&1 wiki-dev.bash-hackers.org/scripting/obsolete May 23 '19 at 1:02
  • 5
    @multithr3at3d A Bashism incompatible with POSIX? Sure! Deprecated or obsolete? No, regardless of what the three authors of this list last edited in 2015 think. It is a short form, and shortening command lines is exactly OP’s goal here. If someone runs a #!/bin/bash script in a different shell without checking for portability first it’s definitely not Bash’s fault if it doesn’t work.
    – dessert
    May 23 '19 at 7:46

I wrote a function and put it into my .bashrc to run things detached from my terminal:

detach () 
    ( "$@" &> /dev/null & )

... and then:

detach google-chrome
detach xdg-open ~/Reference/topic_regex.md

And because I'm lazy, I also wrote a shortcut for xdg-open:

xo () 
    for var in "$@"; do
        detach xdg-open "$var";

Because xdg-open expects exactly one argument, the function xo iterates over all given arguments and calls xdg-open for each one separately.

This allows for:

detach google-chrome
xo . ~/Reference/topic_regex.md

You could redirect the output for all subsequent commands with

exec 1>/dev/null
exec 2>/dev/null

You could create a loop and give it the commands as arguments:

for i in google-chrome "xdg-open ." "code ~/Programs/"; do
  $i &
done &>/dev/null

Note that this way exactly as with the subshell approach with curly braces it’s possible to sum up the outputs and redirect all of them at once.


I created a special file like /dev/null with a shorter path to shorten the redirection. I used /n as the new file’s path:

sudo mknod -m 0666 /n c 1 3

This way you can shorten the individual lines to e.g.:

google-chrome &>/n &

Source: How to create /dev/null?

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.