views:

951

answers:

6

I'd like to define an alias that runs the following two commands consecutively.

gnome-screensaver
gnome-screensaver-command --lock

Right now I've added

alias lock='gnome-screensaver-command --lock'

to my .bashrc but since I lock my workstation so often it would be easier to just type one command.

+3  A: 

Does this not work?

alias whatever='gnome-screensaver ; gnome-screensaver-command --lock'
Sean Bright
+3  A: 

This would run the 2 commands one after another:

alias lock='gnome-screensaver ; gnome-screensaver-command --lock'
Adnan
+2  A: 

So use a semi-colon:

alias lock='gnome-screensaver; gnome-screen-saver-command --lock'

This doesn't work well if you want to supply arguments to the first command. Alternatively, create a trivial script in your $HOME/bin directory.

Jonathan Leffler
+4  A: 

Try:

alias lock='gnome-screensaver; gnome-screensaver-command --lock'

or

lock() {
    gnome-screensaver
    gnome-screensaver-command --lock
}

in your .bashrc

The second solution allows you to use arguments.

mouviciel
shouldn't that be "function lock() { blah }" ?
emeraldjava
I use `sh` syntax, which works with `bash` as well.
mouviciel
+1  A: 

Aliases are meant for aliasing command names. Anything beyond that should be done with functions.

lhunath
+4  A: 

The other answers answer the question adequately, but your example looks like the second command depends on the first one being exiting successfully. You may want to try a short-circuit evaluation in your alias:

alias lock='gnome-screensaver && gnome-screensaver-command --lock'

Now the second command will not even be attempted unless the first one is successful. A better description of short-circuit evaluation is described in this SO question.

gpojd