HOWTO make aliases for frequently used commands

From LinuxReviews
Jump to navigationJump to search

An alias allows you to define shortcuts and synonyms for commonly used shell commands. They are simply set up by using the alias command.

Basic syntax

"alias [-p] [name[=value] ...]

Alias with no arguments or with the -p option prints the list of aliases in the form alias name=value on standard output. When arguments are supplied, an alias is defined for each name whose value is given. A trailing space in value causes the next word to be checked for alias substitution when the alias is expanded. For each name in the argument list for which no value is supplied, the name and value of the alias is printed. Alias returns true unless a name is given for which no alias has been defined"

the bash manual

The basic syntax to make a new alias is:

alias newcommand='yourcommand -arguments'

If you want to start xterm according to your preferences with the command term, do something like:

alias term='xterm -ls -fg gray -bg black'

If you want a quick alias like ll for a more informative file listing:

ls -al --color=yes

Starting alias without any options lists the current aliases:

$ alias

alias ls='ll'
alias ls -al --color=yes
alias term='aterm -ls -fg gray -bg black'

Use unalias to remove an alias.

unalias term

You can also make aliases for existing commands. If you want ls to show colors by default, do:

alias ls='ls --color=yes'

These aliases can be put in your login script (.bash_profile or .profile depending on what shell you are using).

Alias with variables

You can not make aliases with variables. But you can make functions, having a function in your .profile/.bashrc will work just like an alias. To use ssh to copy files to a location on a server you can use

sendpic () { scp $*; }

thanks to mina for sharing this fine command

Storing aliases

Your aliases will be lost the moment you close your terminal. The simplest way of storing them permanently is to list them in $HOME/.bashrc where they will be read each time you open a new terminal. Example:

File: $HOME/.bashrc
alias ssh8="ssh -p 8888 "                                                                                                                                                                                                                                            
alias scp8="scp -P 8888 "
alias lock="pyxtrlock"
alias wget='wget -U "Mozilla/5.0 (Windows NT 10.0; rv:64.0) Gecko/20100101 Firefox/64.0"'
alias wgetmobile='wget -U "Mozilla/5.0 (iPhone; CPU iPhone OS 12_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.1 Mobile/15E148 Safari/604.1"'


Add your comment
LinuxReviews welcomes all comments. If you do not want to be anonymous, register or log in. It is free.