user2736286
user2736286

Reputation: 563

Zsh '&> /dev/null &' alias?

I find myself appending &> /dev/null & all the time, so is there a way I can add it as an alias such as bgh, so I can just say something like rails server bgh?

I've tried using global and suffix aliases, but they don't seem to work for what I want. In a case like rails server &> /dev/null &, what is actual terminology for the role of &> /dev/null &? Is it considered a command option or argument?

Thanks

Upvotes: 0

Views: 1668

Answers (2)

Mathias Laurin
Mathias Laurin

Reputation: 96

You can indeed do that with an alias in zsh:

alias -g bgh='&> /dev/null &'

and test

$ man zsh bgh
[1] 18863
$
[1]  + done       man zsh &> /dev/null

Even though I would personally not include the background & into the alias.

Upvotes: 1

Wrikken
Wrikken

Reputation: 70540

~$ bgh() { "$@" &>/dev/null & }
~$ jobs
~$ bgh sleep 2
[1] 5563
~$ jobs
[1]+  Running                 "$@" &>/dev/null &
~$ 
[1]+  Done                    "$@" &>/dev/null

Explicitly installing zsh & running it there does the same thing:

 ~ % bgh() { "$@" &>/dev/null & }    
 ~ % bgh sleep 1
[2] 6482
 ~ % 
[2]  + done       "$@" &> /dev/null

To get it POSIX compliant (the &> may not work in every shell), we can use this:

~$ bgh() { "$@" >/dev/null 2>&1 & }

Upvotes: 4

Related Questions