287

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.

yuriel
  • 3,053
  • 2
  • 19
  • 11

10 Answers10

510

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
  • 66,855
  • 13
  • 106
  • 140
  • 7
    shouldn't that be "function lock() { blah }" ? – emeraldjava Jan 14 '10 at 12:12
  • 2
    How do you pass the argument? Nesting variable 'msg' inside `lock()` parentheses gives error `syntax error near unexpected token `msg'`.. – geotheory Mar 25 '14 at 14:57
  • 11
    Once the function has been defined, it behaves like a command: arguments are on the command line, separated by whitespaces. On the declaration part, arguments are `$1`, `$2`... in the function body. – mouviciel Mar 25 '14 at 15:17
  • how we pass a parameter for this method – Abuzeid Dec 17 '18 at 23:30
  • 2
    Downvote for not including alias line for with params. – Philip Rego Jun 19 '19 at 18:55
  • 4
    @PhilipRego - I would be glad to learn from you and upvote your answer. – mouviciel Jun 20 '19 at 08:57
  • 3
    @PhilipRego How pedantic. You do you know you can edit or provide your own answers right? – Austin Moore May 20 '20 at 16:13
  • @AustinMoore Just because I downvote doesn't mean I have a better answer. It means the answer didn't work for me. However I did some research and wrote my own answer. The edit queue was full when I tried to edit this question. It's not pendantic, alias isn't intuative and can't easily be generalized for similiar questions. – Philip Rego May 21 '20 at 21:10
  • 1
    doesnt seem to work when you have `command &; disown` – Tian Jun 19 '20 at 04:47
  • 2
    @emeraldjava, not really, see: https://stackoverflow.com/a/7917086/1066240 Although using `lock()` and `function lock()` looks like be totally optional in reality it isn't especially for some older shells. – biesior Sep 11 '20 at 01:23
  • 1
    Note that since this answer was posted in 2009, the `~/.bashrc` files in Ubuntu now usually have a block like `if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi` . Thus they recommend we define our aliases in `~/.bash_aliases` so that `~/.bashrc` stays smaller and simpler. As `~/.bashrc` is always executed at the start of login shells, any aliases you define in `~/.bash_aliases` will be present in your shell when you log in, as long as your `~/.bashrc` `source`s `~/.bash_aliases`. Note that you may have to create the `~/.bash_aliases` file if it doesn't already exist (it didn't on mine). – Nick Muise Jun 05 '23 at 15:22
109

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.

Community
  • 1
  • 1
gpojd
  • 22,558
  • 8
  • 42
  • 71
29

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

alias ll='ls -l' # The ll command is an alias for ls -l

Aliases are names that are still associated with the original name. ll is just a slightly specific kind of ls.

d() {
    if exists colordiff; then
        colordiff -ur "$@"
    elif exists diff; then
        diff -ur "$@"
    elif exists comm; then
        comm -3 "$1" "$2"
    fi | less
}

A function is a new command that has internal logic. It isn't simply a rename of another command. It does internal operations.

Technically, aliases in the Bash shell language are so limited in capabilities that they are extremely ill suited for anything that involves more than a single command. Use them for making a small mutation of a single command, nothing more.

Since the intention is to create a new command that performs an operation which internally will resolve in other commands, the only correct answer is to use a function here:

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

Usage of aliases in a scenario like this runs into a lot of issues. Contrary to functions, which are executed as commands, aliases are expanded into the current command, which will lead to very unexpected issues when combining this alias "command" with other commands. They also don't work in scripts.

lhunath
  • 120,288
  • 16
  • 68
  • 77
  • It would be best if you could provide any example with your answer. waiting for update. – Sajid Ali Nov 15 '17 at 14:40
  • 1
    Downvote for not including alias line for with params. – Philip Rego Jun 19 '19 at 18:56
  • 2
    @PhilipRego aliases do not take parameters. Don't down-vote the apple for not being orange. Eat an orange instead. As the answer explains very well, the right tool here is not aliases but functions. – lhunath Jun 20 '19 at 19:52
  • I mean parameters like this. I was using nested quotes wrong. alias="git commit -m 'init '; git push; git status" – Philip Rego Jun 20 '19 at 21:42
  • @PhilipRego You really need to use a function, not an alias. `gps() { git commit -m 'init '; git push; git status; }` As explained, aliases are extremely limited, fragile and their only intention is to rename commands. Abusing them for unrelated purposes will land you in hot water, such as you just experienced. – lhunath Jun 21 '19 at 22:03
13

Does this not work?

alias whatever='gnome-screensaver ; gnome-screensaver-command --lock'
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
6

This would run the 2 commands one after another:

alias lock='gnome-screensaver ; gnome-screensaver-command --lock'
Adnan
  • 2,949
  • 4
  • 29
  • 45
3

Adding my 2 cents to the 11 year old discussion try this:

alias lock="gnome-screensaver \gnome-screensaver-command --lock"

Neenus
  • 134
  • 3
  • 12
3

Add this function to your ~/.bashrc and restart your terminal or run source ~/.bashrc

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

This way these two commands will run whenever you enter lock in your terminal.

In your specific case creating an alias may work, but I don't recommend it. Intuitively we would think the value of an alias would run the same as if you entered the value in the terminal. However that's not the case:

The rules concerning the definition and use of aliases are somewhat confusing.

and

For almost every purpose, shell functions are preferred over aliases.

So don't use an alias unless you have to. https://ss64.com/bash/alias.html

Philip Rego
  • 552
  • 4
  • 20
  • 35
2

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
  • 730,956
  • 141
  • 904
  • 1,278
1
function lock() {
    gnome-screensaver
    gnome-screensaver-command --lock
}

The above translated perfectly for me in bash as:

bottom() {
    clear
    printf '\n%.0s' {1..100}
}
alias c="bottom"

All I wanted to do was clear the screen (c ) and have the bash prompt appear at the bottom, not top of terminal window. I had solved this long ago (too long ago... forgot what I did), but now I've put the function in .bash_profile and it's off to the races! For now, I am also executing the function so that when I open a new term. window, the prompt, and only the prompt, appears at the bottom. Thanks much for the suggestion. I'm not sure if I just miss this kind of stuff or miss getting paid for it... probably both. :-)

Yeheshuah
  • 1,216
  • 1
  • 13
  • 28
0

On windows, in Git\etc\bash.bashrc I use (at the end of the file)

a(){
    git add $1  
    git status
}

and then in git bash simply write

$ a Config/
Andy
  • 13
  • 3