16

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?

Melebius
  • 11,121
  • 8
  • 50
  • 77
AbstProcDo
  • 2,811
  • 6
  • 29
  • 56
  • 2
    You can put all or most of those programs in your [Startup Applications](https://help.ubuntu.com/stable/ubuntu-help/startup-applications.html.en). – Dennis Williamson 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

5 Answers5

17

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.

Melebius
  • 11,121
  • 8
  • 50
  • 77
  • Do note that the `&>` syntax is deprecated and should be replaced with `>/dev/null 2>&1` http://wiki-dev.bash-hackers.org/scripting/obsolete – multithr3at3d May 23 '19 at 01: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 07:46
  • I assume that i can not have it on s!ngle line.. – 16851556 Oct 13 '22 at 04:30
14

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";
    done
}

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
PerlDuck
  • 13,014
  • 1
  • 36
  • 60
5

You could redirect the output for all subsequent commands with

exec 1>/dev/null
exec 2>/dev/null
ohno
  • 823
  • 4
  • 9
3

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?

dessert
  • 39,392
  • 12
  • 115
  • 163
3

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.

dessert
  • 39,392
  • 12
  • 115
  • 163