0

I often want to have some kind of alias/substitution of 'bits' of a command.

ex: I want 'bkg' to translate to "2> /dev/null > /dev/null &" so that I can run applications in the background and not have their output spam the console like:

nautilus . bgk
>>>
nautilus . 2> /dev/null > /dev/null &

The closest thing I got to work so far is the following:

1) Create alias:

alias bkg="&> /dev/null > /dev/null &"

2) Before typing command, type alias and first expand it via ctrl+alt+e, then go back and type command.

bkg       #press ctrl+alt+e
&> /dev/null > /dev/null &    #has expanded. Now go back and type cmd:
nautilus &> /dev/null > /dev/null &

However, this is somewhat cumbersome.

[edit] ZSH seems to have global aliases. This is close to what I need. But from readings it appears that zsh is not fully compatible with bash scripts. It seems that switching to zsh only for global alising but breaking bash scripts might not be worth the effort. As such, looking for bash-native solution.

[edit]
As per @egmont's suggestion, something close to what I need is to define an alias:

alias quiet="&> /dev/null "

Then I can use it like:

quiet nautilus . &

However, two issues: "&" has to be at the end and tab-prediction doesn't work after 'quiet' is typed.

1

ZSH will give you all the rope you need with global aliases (and also &>/dev/null as a shorter way to write the redirection).

% alias -g bgk='&>/dev/null &'
% sleep 20 bgk
[1] 44612
% 

However, I strongly recommend against using global aliases, as they are something of a footgun.

1

A little known feature of shells is that redirection doesn't need to stand at the end of the line, it can be anywhere in between the arguments or even at the very beginning. In other words, these three are equivalent:

nautilus . >/dev/null
nautilus >/dev/null .
>/dev/null nautilus .

As such, you can define alias blk='>/dev/null' which will work at the beginning of the command line, or define alias naublk='nautlius >/dev/null' and then execute naublk ., and so on.

Interestingly, in bash-4.3 it seems that you can have multiple aliases for redirections at the beginning, followed by one for the command. E.g.:

alias dn1='> /dev/null'
alias dn2='2> /dev/null'
alias nau=nautilus
dn1 dn2 nau .
  • This is close to what I need. – Leo Ufimtsev Dec 14 '16 at 16:23
  • I edited my question to outline only issue of this approach. – Leo Ufimtsev Dec 14 '16 at 16:25

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.