Multiple commands in an alias for bash

BashConfiguration

Bash Problem Overview


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.

Bash Solutions


Solution 1 - Bash

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.

Solution 2 - Bash

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.

Solution 3 - Bash

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.

Solution 4 - Bash

Does this not work?

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

Solution 5 - Bash

This would run the 2 commands one after another:

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

Solution 6 - Bash

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

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

Solution 7 - Bash

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

Solution 8 - Bash

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.

Solution 9 - Bash

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 [tag:alias]) 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. :-)

Solution 10 - Bash

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/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionyurielView Question on Stackoverflow
Solution 1 - BashmouvicielView Answer on Stackoverflow
Solution 2 - BashgpojdView Answer on Stackoverflow
Solution 3 - BashlhunathView Answer on Stackoverflow
Solution 4 - BashSean BrightView Answer on Stackoverflow
Solution 5 - BashAdnanView Answer on Stackoverflow
Solution 6 - BashNeenusView Answer on Stackoverflow
Solution 7 - BashPhilip RegoView Answer on Stackoverflow
Solution 8 - BashJonathan LefflerView Answer on Stackoverflow
Solution 9 - BashBruce SmithView Answer on Stackoverflow
Solution 10 - BashAndyView Answer on Stackoverflow