Manjaro Difference between revisions of "Aliases in .bashrc"

Difference between revisions of "Aliases in .bashrc"

From Manjaro
imported>Fhdk
(remove yay)
(Marked this version for translation)
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
<languages/>
== What's .bashrc? What is an alias? ==
__TOC__
 
<translate>
 
==What's a bashrc? What's a alias?== <!--T:1-->
'''.bashrc''' is the ''configuration file'' for bash, a linux shell/command interpreter.
* '''.bashrc''' is the ''configuration file'' for bash, a linux shell/command interpreter.
 
* An '''alias''' is a ''substitute for a (complete) command''. It can be thought of as a shortcut.
An '''alias''' is a ''substitute for a (complete) command''. It can be thought of as a shortcut.
* '''.bashrc''' is found in the ''home folder'' of a user ( ~ ) . It is a hidden file, to see it show hidden files in your file manager or use '''ls -a'''
 
==Backup your current ~/.bashrc==
'''.bashrc''' is found in the ''home folder'' of a user ( ~ ) . It is a hidden file, to see it show hidden files in your file manager or use '''ls -a'''
It can be useful to backup the ~/.bashrc before editing it, as it allows one to be able to easily recover from the unexpected. To make a backup of your current .bashrc . Open a terminal and execute:
 
{{UserCmd|command=cp ~/.bashrc ~/.bashrc.bak}}
== Backup your current .bashrc ==
The original .bashrc can be restored with by executing
 
{{UserCmd|command=cp -i ~/.bashrc.bak ~/.bashrc}}
It can be useful to backup the ~/.bashrc before editing it, as it allows one to be able to easily recover from the unexpected.
==Note==
 
Any changes made to the ~/.bashrc will have no effect on any currently open terminal windows. To test newly updated changes in your ~/.bashrc open a new terminal or use the command:
To make a backup of your current .bashrc . Open a terminal and type -
{{UserCmd|command=source ~/.bashrc}}
 
cp ~/.bashrc ~/.bashrc.bak
 
The original .bashrc can be restored with -
 
cp -i ~/.bashrc.bak ~/.bashrc
 
== Note ==
 
Any changes made to the .bashrc will have no effect on any currently open terminal windows. To test newly updated changes in your .bashrc open a new terminal or use the command:
 
source .bashrc
 
== Aliases Examples ==
== Aliases Examples ==
Aliases can turn a complex command string into a simple custom made command that one can type in the Terminal.  
Aliases can turn a complex command string into a simple custom made command that one can type in the Terminal.  
 
=== Standard syntax ===
The following can be added to the .bashrc file.
Creating aliases in bash is very straight forward. The syntax is as follows:
 
{{File|file=~/.bashrc|
content=<pre>...
alias alias_name="command_to_run"
...</pre>}}
=== For updating your system ===
=== For updating your system ===
 
To upgrade the system via pacman, the command used is  
To upgrade the system via pacman, the command used is '''''sudo pacman -Syu'''''
{{UserCmd|command=sudo pacman -Syu}}
 
This can be aliased in ~/.bashrc with
This can be aliased in .bashrc with-
{{File|file=~/.bashrc|
 
content=<pre>...
alias pacup="sudo pacman -Syu"  
alias pacup="sudo pacman -Syu"
 
...</pre>}}
 
To upgrade packages installed from the AUR via pamac, the command used is  
To upgrade packages installed from the AUR via pamac, the command used is '''''pamac upgrade --aur'''''
{{UserCmd|command=pamac upgrade --aur}}
 
This can be aliased with
This can be aliased with-
{{File|file=~/.bashrc|
 
content=<pre>...
alias aup="pamac upgrade --aur"  
alias aup="pamac upgrade --aur"
 
...</pre>}}
 
=== For editing commonly used files ===
=== For editing commonly used files ===
 
To edit '''~/.bashrc''' itself and automatically reload bash configuration file (so that changes made to .bashrc can be implemented in current terminal session)
 
{{File|file=~/.bashrc|
To edit '''.bashrc''' itself and automatically reload bash configuration file (so that changes made to .bashrc can be implemented in current terminal session)
content=<pre>...
 
alias bashrc="nano ~/.bashrc && source ~/.bashrc"
alias bashrc="nano ~/.bashrc && source ~/.bashrc"  
...</pre>}}
 
 
To edit '''/etc/fstab'''
To edit '''/etc/fstab'''
 
{{File|file=~/.bashrc|
alias fstab="sudo nano /etc/fstab"
content=<pre>...
 
alias fstab="sudo nano /etc/fstab"
 
...</pre>}}
To edit '''/etc/default/grub'''
To edit '''/etc/default/grub'''
 
{{File|file=~/.bashrc|
alias grub="sudo nano /etc/default/grub"
content=<pre>...
 
alias grub="sudo nano /etc/default/grub"
...</pre>}}
=== To update GRUB ===
=== To update GRUB ===
To update your grub bootloader using the '''sudo update-grub'''
To update your grub bootloader using the '''sudo update-grub'''
{{File|file=~/.bashrc|
content=<pre>...
alias grubup="sudo update-grub"
...</pre>}}
==Creating Bash Aliases with Arguments (Bash Functions)==
Sometimes you may need to create an alias that accepts one or more arguments. That’s where bash functions come in handy.


alias grubup="sudo update-grub"
<!--T:2-->
 
The syntax for creating a bash function is very easy. They can be declared in two different formats:
== Conclusion ==
{{File|file=~/.bashrc|
content=<pre>...
function_name () {
  [commands]
}
...</pre>}}
or
{{File|file=~/.bashrc|
content=<pre>...
function function_name {
  [commands]
}
...</pre>}}
To pass any number of arguments to the bash function simply, put them right after the function’s name, separated by a space. The passed parameters are $1, $2, $3, etc., corresponding to the position of the parameter after the function’s name. The $0 variable is reserved for the function name.


Let’s create a simple bash function which will create a directory and then navigate into it:
{{File|file=~/.bashrc|
content=<pre>...
mkcd ()
{
  mkdir -p -- "$1" && cd -P -- "$1"
}
...</pre>}}
Now instead of using mkdir to create a new directory and then cd to move into that directory , you can simply type:
{{UserCmd|command=mkcd new_directory}}
==Keeping bash alias in a different file== <!--T:3-->
Bash allows you to add local aliases in your ~/.bashrc file. To do this create a file called ~/.bash_aliases and add these contents in your ~/.bashrc file:
{{File|file=~/.bashrc|
content=<pre>...
if [ -e $HOME/.bash_aliases ]; then
    source $HOME/.bash_aliases
fi
...</pre>}}
Now you can add any aliases in your ~/.bash_aliases file and then load them into your Bash session with the source ~/.bashrc command.
==Conclusion==
This list is not comprehensive. Almost anything that is commonly used can be shortened with an alias
This list is not comprehensive. Almost anything that is commonly used can be shortened with an alias
 
==See Also==
 
== See Also ==
[https://www.gnu.org/software/bash/manual/html_node/index.html Bash documentation]
[https://www.gnu.org/software/bash/manual/html_node/index.html Bash documentation]
 
[https://wiki.archlinux.org/title/bash#Aliases ArchWiki]
 
</translate>
[[Category:Contents Page]]
[[Category:Contents Page{{#translation:}}]]
[[Category:Terminal{{#translation:}}]]

Latest revision as of 16:14, 29 December 2022

Other languages:
English • ‎Türkçe • ‎português do Brasil • ‎русский

What's a bashrc? What's a alias?

  • .bashrc is the configuration file for bash, a linux shell/command interpreter.
  • An alias is a substitute for a (complete) command. It can be thought of as a shortcut.
  • .bashrc is found in the home folder of a user ( ~ ) . It is a hidden file, to see it show hidden files in your file manager or use ls -a

Backup your current ~/.bashrc

It can be useful to backup the ~/.bashrc before editing it, as it allows one to be able to easily recover from the unexpected. To make a backup of your current .bashrc . Open a terminal and execute:

user $ cp ~/.bashrc ~/.bashrc.bak COPY TO CLIPBOARD


The original .bashrc can be restored with by executing

user $ cp -i ~/.bashrc.bak ~/.bashrc COPY TO CLIPBOARD


Note

Any changes made to the ~/.bashrc will have no effect on any currently open terminal windows. To test newly updated changes in your ~/.bashrc open a new terminal or use the command:

user $ source ~/.bashrc COPY TO CLIPBOARD


Aliases Examples

Aliases can turn a complex command string into a simple custom made command that one can type in the Terminal.

Standard syntax

Creating aliases in bash is very straight forward. The syntax is as follows:

~/.bashrc
...
alias alias_name="command_to_run"
...

For updating your system

To upgrade the system via pacman, the command used is

user $ sudo pacman -Syu COPY TO CLIPBOARD


This can be aliased in ~/.bashrc with

~/.bashrc
...
alias pacup="sudo pacman -Syu"
...

To upgrade packages installed from the AUR via pamac, the command used is

user $ pamac upgrade --aur COPY TO CLIPBOARD


This can be aliased with

~/.bashrc
...
alias aup="pamac upgrade --aur"
...

For editing commonly used files

To edit ~/.bashrc itself and automatically reload bash configuration file (so that changes made to .bashrc can be implemented in current terminal session)

~/.bashrc
...
alias bashrc="nano ~/.bashrc && source ~/.bashrc"
...

To edit /etc/fstab

~/.bashrc
...
alias fstab="sudo nano /etc/fstab"
...

To edit /etc/default/grub

~/.bashrc
...
alias grub="sudo nano /etc/default/grub"
...

To update GRUB

To update your grub bootloader using the sudo update-grub

~/.bashrc
...
alias grubup="sudo update-grub"
...

Creating Bash Aliases with Arguments (Bash Functions)

Sometimes you may need to create an alias that accepts one or more arguments. That’s where bash functions come in handy.

The syntax for creating a bash function is very easy. They can be declared in two different formats:

~/.bashrc
...
function_name () {
  [commands]
}
...

or

~/.bashrc
...
function function_name {
  [commands]
}
...

To pass any number of arguments to the bash function simply, put them right after the function’s name, separated by a space. The passed parameters are $1, $2, $3, etc., corresponding to the position of the parameter after the function’s name. The $0 variable is reserved for the function name.

Let’s create a simple bash function which will create a directory and then navigate into it:

~/.bashrc
...
mkcd ()
{
  mkdir -p -- "$1" && cd -P -- "$1"
}
...

Now instead of using mkdir to create a new directory and then cd to move into that directory , you can simply type:

user $ mkcd new_directory COPY TO CLIPBOARD


Keeping bash alias in a different file

Bash allows you to add local aliases in your ~/.bashrc file. To do this create a file called ~/.bash_aliases and add these contents in your ~/.bashrc file:

~/.bashrc
...
if [ -e $HOME/.bash_aliases ]; then
    source $HOME/.bash_aliases
fi
...

Now you can add any aliases in your ~/.bash_aliases file and then load them into your Bash session with the source ~/.bashrc command.

Conclusion

This list is not comprehensive. Almost anything that is commonly used can be shortened with an alias

See Also

Bash documentation ArchWiki

Cookies help us deliver our services. By using our services, you agree to our use of cookies.