Multiple commands in an alias for bash

前端 未结 9 2014
[愿得一人]
[愿得一人] 2020-11-29 15:05

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

gnome-screensaver
gnome-screensaver-command --lock

Right

9条回答
  •  盖世英雄少女心
    2020-11-29 15:46

    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

提交回复
热议问题