- What is Linux alias command and how to use it
- What is an alias in linux?
- Syntax of alias command in linux
- How to create alias in linux
- Removing alias from the system
- how to create a permanent alias in linux
- Comments
- Linux alias Command: How to Use It With Examples
- What Is an Alias in Linux?
- Linux Alias Syntax
- Create Aliases in Linux
- Create a Temporary Alias in Linux
- Create a Permanent Alias in Linux
- List All Aliases in Linux
- Remove Aliases in Linux
What is Linux alias command and how to use it
Linux Command Alias
Most Linux users are well versed in the typing out of lengthy commands in the Terminal. They’re especially hardened and generally insured against such sudden lapses in concentration, where they have to go back mid-command to correct an error.
The rest of us, however, have to deal with the fact that an error is inevitable and that we more often than not have to enter the command again to get it working correctly. There is another option available for Linux users, though. Instead of constantly or frequently having to tap out the complex line of instructions, we can instead use an Alias.
The Alias command isn’t just for Linux; there are examples of it being used via the DOS Key command in the past with DOS and earlier versions of Windows, and more recently appearing in the Windows PowerShell. However, it was introduced back in the old UNIX days and made life significantly simpler for the users of the time.
What is an alias in linux?
The Alias command can be issued to replace a common command with a shorter word. So where in Linux the command ‘sudo apt-get install’ needs to be inputted, an alias can be set up so all the user has to do is type in ‘install’, for example. This will then last for as long as the user is in the Terminal for, but they can be set as permanent, which we’ll get into later on.
Create an alias, aliases allow a string to be substituted for a word when it is used as the first word of a simple command.
Syntax alias [-p] [name[=value] …] unalias [-a] [name … ] Key -p Print the current values -a Remove All aliases
If arguments are supplied, an alias is defined for each name whose value is given.
If no value is given, alias will print the current value of the alias.
Without arguments or with the -p option, alias prints the list of aliases on the standard output in a form that allows them to be reused as input.
The value cannot contain any positional parameters ($1 etc), if you need to do that use a shell function instead.
The name may not be ‘alias’ or ‘unalias’.
unalias may be used to remove each name from the list of defined aliases.
Make an alias permanent
Use your favorite text editor to create a file called /.bash_aliases, and type the alias commands into the file.
.bash_aliases will run at login (or you can just execute it with ..bash_aliases )
Expand Multiple aliases
If the last character of the alias value is a space or tab character, then the next command word following the alias is also checked for alias expansion.
Examples: Create an alias ‘c’ that will clear the screen:
Syntax of alias command in linux
How to create alias in linux
In general, the creation of alias is as follows:
alias name=value alias name='command' alias name='command arg1 arg2' alias name='/path/to/script' alias name='/path/to/script.pl arg1'
That is, we write the name of the alias, put the “=” sign, then indicate the command or list of commands in quotation marks with arguments. Instead of console commands, we can specify the path to the script with or without arguments. Let’s look at examples.
In order to completely update the system, we need to enter the commands:
sudo apt update && sudo apt full-upgrade
Each time to write this for a long time, we can create an alias:
alias upd='sudo apt update && sudo apt full-upgrade'
Let’s redefine the ping command by setting the parameter so that only 4 packets are sent when it is used:
How to invoke a command that has been overridden by alias.
But what if we want to use the old ping command? This can also be done, you need to enter the backslash “” before the command:
In addition, in addition to the backslash, there are other ways to call the original command:
That is, by enclosing in quotation marks or by entering the keyword “command” before the command.
Removing alias from the system
The unalias command and the alias name are used for deletion.linux remove alias In general terms, it looks:
For example, I mistakenly added alias: X and i want to remove it i would type this comman
and if you want to delete all aliases; for this, use the command:
That is, with the -a option .
Note:After closing the terminal, all our aliases (aliases, aliases) are deleted, except for system ones. That is, these aliases exist only within the framework of the current terminal session.
If you added alias in one tab (window) of the terminal on another tab (in another terminal session), the system knows nothing about this alias but this can be solved by creating permanent aliases.
how to create a permanent alias in linux
permanent alias linux I’ll go from afar, in the user’s home directory there is a file .bashrc This file is used in the system when this user is authorized. That is, our login. There are lines in this file:
# Alias definitions. You may want to put all your additions into a separate file like ~/.bash_aliases, instead of adding them here directly. See /usr/share/doc/bash-doc/examples in the bash-doc package. if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi
This means that if the .bash_aliases file exists , then it is used in the system. We will create this file and write to it a list of our aliases so that they are always available. But unlike Kali Linux or Ubuntu, for example, in Linux Mint this entry may be missing, so add these lines to your .bashrc at the end of the file.
In order not to restart the system and the changes to this file are applied, we execute the command:
We figured it out. Let’s create a ~/bash_aliases file. It can be created in the file manager, superuser rights are not required. Or we enter in the terminal:
In this file, insert the list of aliases that we consider necessary:
Save the file, If everything was done correctly, now these aliases will be available in any terminal window of the current user. And also after reboot will be available.
This article has been detailing the creation of alias in Kali linux. If you use this convenient tool, you can share your set of aliases in the comments.
For more linux command check Linux basic commands
Comments
Linux alias Command: How to Use It With Examples
Depending on the type of work you do on your Linux system, you may need to enter the same long and complicated commands frequently. The alias command lets you create shortcuts for these commands, making them easier to remember and use.
In this tutorial, we will show you how you can create, review, and remove command aliases in Linux.
- A system running a Linux distribution
- An account with sudo privileges
- Access to the terminal window or command line
- A text editor, such as Vim or nano
What Is an Alias in Linux?
In Linux, an alias is a shortcut that references a command. An alias replaces a string that invokes a command in the Linux shell with another user-defined string.
Aliases are mostly used to replace long commands, improving efficiency and avoiding potential spelling errors. Aliases can also replace commands with additional options, making them easier to use.
Linux Alias Syntax
The alias command uses the following syntax:
The different elements of the alias command syntax are:
- alias : Invokes the alias command.
- [option] : Allows the command to list all current aliases.
- [name] : Defines the new shortcut that references a command. A name is a user-defined string, excluding special characters and ‘alias’ and ‘unalias’, which cannot be used as names.
- [value] : Specifies the command the alias references. Commands can also include options, arguments, and variables. A value can also be a path to a script you want to execute.
Note: Enclosing the value in single quotation marks (‘) will not expand any variables used with the command. To expand the variables, use double quotation marks («).
Create Aliases in Linux
There are two types of aliases to create in Linux:
- Temporary. Add them using the alias command.
- Permanent. These require to edit system files.
Create a Temporary Alias in Linux
Use the alias command to create a temporary alias that lasts until the end of the current terminal session. For instance, creating c as an alias for the clear command:
Note: The alias command allows you to include multiple commands as the value by dividing them with the pipe symbol (|).
If you want to reference any additional command options when creating an alias, include them as a part of the value. For example, adding move as an alias for the mv command with the option of asking for confirmation before overwriting:
Note: Learn more about the mv command in our guide to moving directories in Linux.
Another use for aliases is to create a shortcut for running scripts. To do this, provide the absolute path to the script as the value:
alias frename='Example/Test/file_rename.sh'
In this example, using frename as a command runs the file_rename.sh bash script.
Create a Permanent Alias in Linux
To make an alias permanent, you need to add it to your shell configuration file. Depending on the type of shell you are using, use:
Start by opening the shell configuration file in a text editor. In this example, we are using the Bash shell and nano text editor:
Scroll down until you find a section that lists default system aliases. For ease of use, create a separate section with a descriptive comment and add your aliases using the alias command syntax.
#Custom aliases alias c='clear' alias move='mv -i' alias frename='Example/Test/file_rename.sh'
Once you add all of the new alises, press Ctrl+X, type Y and press Enter to save the changes to the configuration file.
The new aliases automatically load in the next terminal session. If you want to use them in the current session, load the configuration file using the source command:
List All Aliases in Linux
Using the alias command on its own displays a list of all currently set aliases:
Another method is to add the -p flag. This option displays the list in a format suitable for input to the shell:
Remove Aliases in Linux
To remove an alias, use the unalias command with the following syntax:
For instance, to remove the frename alias:
Adding the -a option allows you to remove all aliases:
The example above shows how alias does not return any results after the unalias -a command.
After reading this tutorial, you should be able to use the alias command to create and manage aliases on your Linux system. This will help streamline your work and make terminal commands easier to use.
To learn more about other commands in Linux, check out our Linux commands cheat sheet.