- Location of «~/.bash_aliases»
- 2 Answers 2
- Different Ways to Create and Use Bash Aliases in Linux
- Check Bash Aliases in Linux
- Creating Alias in Linux
- Removing an Alias in Linux
- Adding System-Wide Aliases
- Where Is The Alias File In Linux For Persistency?
- Alias File In Bash Shell
- Alias File In ZSH Shell
- Alias File In CSH Shell
- Alias File In Korn Shell
- Adding Alias To Alias Files
Location of «~/.bash_aliases»
I want to make permanent aliases in Terminal, and I have read this answer on how to do it: https://askubuntu.com/a/5278/364819 But I have got a small problem, I have found the code:
if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi
As refereed to in the answer. But I cannot actually find the .bash_aliases file which I can set these permanent aliases in. So my question is, where is the location of this file, and if I need to create it, do I just create it in my home user directory directory? I am running on Ubuntu 14.10.
If it’s not there in your home directory, feel free to create the file. That’s why the code snipped first tests for file existence before trying to source it.
2 Answers 2
Yes, just create it in your home directory.
touch ~/.bash_aliases or open an empty file in a text editor, e.g.,
This should be one of your first aliases..
##### ea - alias for editing aliases # #When setting up a new aliases file, or having creating a new file.. About every time after editing an aliases file, I source it. This alias makes editing alias a #bit easier and they are useful right away. Note if the source failed, it will not echo "aliases sourced". # #Sub in gedit for your favorite editor, or alter for ksh, sh, etc. # alias ea='gedit ~/.bash_aliases; source ~/.bash_aliases && source $HOME/.bash_aliases && echo "aliases sourced --ok."' #
When you run across something that would be a good alias, enter ea , a text editor opens. Add your new alias. Close the editor. The alias automagically sources; makes the new alias immediately available; and you’re on your way.
Different Ways to Create and Use Bash Aliases in Linux
Alias in bash can be termed simply as a command or a shortcut that will run another command/program. Alias is very helpful when our command is very long and for frequently used commands. Over the course of this article, we are going to see how powerful is an alias and the different ways to set up an alias and use it.
Check Bash Aliases in Linux
Alias is a shell builtin command and you can confirm it by running:
$ type -a alias alias is a shell builtin
Before jumping and setting up an alias we will see the configuration files involved. An alias can be set either at the “user-level” or “system level”.
Invoke your shell and simply type “alias” to see the list of defined alias.
User-level aliases can be defined either in the .bashrc file or the .bash_aliases file. The .bash_aliases file is to group all your aliases into a separate file instead of putting it in the .bashrc file along with other parameters. Initially, .bash_aliases will not be available and we have to create it.
$ ls -la ~ | grep -i .bash_aliases # Check if file is available $ touch ~/.bash_aliases # Create empty alias file
Open the .bashrc file and look out for the following section. This section of code is responsible for checking if file .bash_aliases is present under the user home directory and load it whenever you initiate a new terminal session.
# 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
You can also create a custom alias file under any directory and add definition in either .bashrc or .profile to load it. But I will not prefer this and I choose to stick with grouping all my alias under .bash_aliases.
You can also add aliases under the .bashrc file. Look out for the alias section under the .bashrc file where it comes with some predefined aliases.
# enable color support of ls and also add handy aliases if [ -x /usr/bin/dircolors ]; then test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)" alias ls='ls --color=auto' #alias dir='dir --color=auto' #alias vdir='vdir --color=auto' alias grep='grep --color=auto' alias fgrep='fgrep --color=auto' alias egrep='egrep --color=auto' fi # colored GCC warnings and errors #export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01' # some more ls aliases alias ll='ls -alF' alias la='ls -A' alias l='ls -CF' # Add an "alert" alias for long running commands. Use like so: # sleep 10; alert alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*9\+\s*//;s/[;&|]\s*alert$//'\'')"'
Creating Alias in Linux
You can either create a temporary alias that will be stored only for your current session and will be destroyed once your current session ends or permanent alias which will be persistent.
The syntax for creating an alias in Linux.
For example, in a real scenario.
Open the terminal and create any alias command you desire. If you open another session then the newly created alias will not be available.
$ alias Hello"echo welcome to Tecmint" $ alias $ Hello
To make the alias persistent, add it to the .bash_aliases file. You can use your favorite text editor or use the cat command or echo command to add an alias.
$ echo alias nf="neofetch" >> ~/.bash_aliases $ cat >> ~/.bash_aliases $ cat ~/.bash_aliases
You have to reload the .bash_aliases file for the changes to be effective in the current session.
Now if I run “nf” which is an alias for “neofetch” it will trigger the neofetch program.
An alias can come in handy if you wish to override the default behavior of any command. For demonstration, I will take an uptime command, that will display system uptime, the number of users logged in, and the system load average. Now I will create an alias that will override the behavior of the uptime command.
$ uptime $ cat >> ~/.bash_aliases alias uptime="echo 'I am running uptime command now'" $ source ~/.bash_aliases $ uptime
From this example, you can conclude the precedence falls to bash aliases before checking and invoking the actual command.
$ cat ~/.bash_aliases $ source ~/.bash_aliases $ uptime
Removing an Alias in Linux
Now remove the uptime entry from the .bash_aliases file and reload the .bash_aliases file which will still print the uptime with alias definition. This is because the alias definition is loaded into the current shell session and we have to either start a new session or unset the alias definition by running the unalias command as shown in the below image.
NOTE: Unalias will remove the alias definition from the currently loaded session, not from .bashrc or .bash_aliases.
Adding System-Wide Aliases
Till this point, we have seen how to set up an alias at the user level. To set an alias globally you can modify the “/etc/bash.bashrc” file and add aliases which will be effective globally. You need to have the elevated privilege to modify bash.bashrc file.
Alternatively, create a script under “/etc/profile.d/”. When you log in to a shell “/etc/profile” will run any script under profile.d before actually running ~/.profile. This method will reduce the risk of messing up either /etc/profile or /etc/bash.bashrc file.
$ sudo cat >> /etc/profile.d/alias.sh alias ls=”ls -ltra”
Below is the code grabbed from the /etc/profile that takes care of running any scripts that we put under /etc/profiles.d/. It will look out for any files with the .sh extension and run the source command.
NOTE: It is best practice to take a backup of user-level or system-level files. If in case something went wrong backup copy can be reverted.
That’s it for this article. We have seen what is alias, the configuration files involved with the alias, and different ways to set up the alias locally and globally.
Where Is The Alias File In Linux For Persistency?
The Linux alias is used to create shortcuts or short forms for the commands. The alias makes a command with options easy to call without remembering all details. If you set an alias but this alias is not saved and cleared after reboots you should use the shell configuration file in order to save the alias. Aliases are created using the alias keyword but where is the alias file or alias file configuration?
Alias File In Bash Shell
The alias commands in bash are stored inside the bash configuration file. The bash configuration file is located ~/.bashrc . We can put the alias configuration into the “.bashrc” file in order to make the alias persistent between reboots.
Alias File In ZSH Shell
The alias file in ZSH shell is located in the ~/.zsh . We can put the alias configuration into the “.zshrc” file in order to make the alias persistent between reboots.
Alias File In CSH Shell
The alias configuration is stored in ~/.csh the CSH shell. We can put the alias configuration into the “.csh” file in order to make the alias persistent between reboots.
Alias File In Korn Shell
The alias configuration is stored in ~/.ksh the CSH shell. We can put the alias configuration into the “.ksh” file in order to make the alias persistent between reboots.
Adding Alias To Alias Files
Open the alias files according to your shell type and add the alias. In the following example, we add an alias named mycd which simply changes the current working directory into the /mnt .