How do I create a permanent Bash alias?
I would like to create an alias to rm
command in order to have a confirmation message after executing this command. So I am creating an alias like this alias rm='rm -i'
. But as far as I know this is a temporary alias and it lives until you close the terminal.
As it is explained here to save alias permanently I need to execute ~/.bash_aliases
or ~/.bashrc
commands in terminal and add my alias there. But when I execute ~/.bashrc
I get following error message :
bash: /home/bakhtiyor/.bashrc: Permission denied
When I run ~/.bash_aliases
I get another error message like this:
bash: /home/bakhtiyor/.bash_aliases: File or directory doesn't exist.
What is the actual problem and how can I solve it?
bash alias
add a comment |
I would like to create an alias to rm
command in order to have a confirmation message after executing this command. So I am creating an alias like this alias rm='rm -i'
. But as far as I know this is a temporary alias and it lives until you close the terminal.
As it is explained here to save alias permanently I need to execute ~/.bash_aliases
or ~/.bashrc
commands in terminal and add my alias there. But when I execute ~/.bashrc
I get following error message :
bash: /home/bakhtiyor/.bashrc: Permission denied
When I run ~/.bash_aliases
I get another error message like this:
bash: /home/bakhtiyor/.bash_aliases: File or directory doesn't exist.
What is the actual problem and how can I solve it?
bash alias
How did you create an alias??
– karthick87
Dec 15 '10 at 8:05
@karthick87. I have updated my question.
– Bakhtiyor
Dec 15 '10 at 8:17
2
ANSWER -- stackoverflow.com/a/2622711/1487102
– neaumusic
Sep 10 '15 at 18:18
add a comment |
I would like to create an alias to rm
command in order to have a confirmation message after executing this command. So I am creating an alias like this alias rm='rm -i'
. But as far as I know this is a temporary alias and it lives until you close the terminal.
As it is explained here to save alias permanently I need to execute ~/.bash_aliases
or ~/.bashrc
commands in terminal and add my alias there. But when I execute ~/.bashrc
I get following error message :
bash: /home/bakhtiyor/.bashrc: Permission denied
When I run ~/.bash_aliases
I get another error message like this:
bash: /home/bakhtiyor/.bash_aliases: File or directory doesn't exist.
What is the actual problem and how can I solve it?
bash alias
I would like to create an alias to rm
command in order to have a confirmation message after executing this command. So I am creating an alias like this alias rm='rm -i'
. But as far as I know this is a temporary alias and it lives until you close the terminal.
As it is explained here to save alias permanently I need to execute ~/.bash_aliases
or ~/.bashrc
commands in terminal and add my alias there. But when I execute ~/.bashrc
I get following error message :
bash: /home/bakhtiyor/.bashrc: Permission denied
When I run ~/.bash_aliases
I get another error message like this:
bash: /home/bakhtiyor/.bash_aliases: File or directory doesn't exist.
What is the actual problem and how can I solve it?
bash alias
bash alias
edited Apr 13 '17 at 12:24
Community♦
1
1
asked Dec 15 '10 at 7:54
BakhtiyorBakhtiyor
4,374185676
4,374185676
How did you create an alias??
– karthick87
Dec 15 '10 at 8:05
@karthick87. I have updated my question.
– Bakhtiyor
Dec 15 '10 at 8:17
2
ANSWER -- stackoverflow.com/a/2622711/1487102
– neaumusic
Sep 10 '15 at 18:18
add a comment |
How did you create an alias??
– karthick87
Dec 15 '10 at 8:05
@karthick87. I have updated my question.
– Bakhtiyor
Dec 15 '10 at 8:17
2
ANSWER -- stackoverflow.com/a/2622711/1487102
– neaumusic
Sep 10 '15 at 18:18
How did you create an alias??
– karthick87
Dec 15 '10 at 8:05
How did you create an alias??
– karthick87
Dec 15 '10 at 8:05
@karthick87. I have updated my question.
– Bakhtiyor
Dec 15 '10 at 8:17
@karthick87. I have updated my question.
– Bakhtiyor
Dec 15 '10 at 8:17
2
2
ANSWER -- stackoverflow.com/a/2622711/1487102
– neaumusic
Sep 10 '15 at 18:18
ANSWER -- stackoverflow.com/a/2622711/1487102
– neaumusic
Sep 10 '15 at 18:18
add a comment |
9 Answers
9
active
oldest
votes
To create an alias permanently add the alias to your .bashrc
file
gedit ~/.bashrc
And then add your alias at the bottom.
Now execute . ~/.bashrc
in your terminal (there should be a space between the .
and ~/.bashrc
.
Now you can check your alias.
4
@karthick87 you wrote "Now execute . ~/.bashrc in your terminal (there should be a gap between the . and ~/.bashrc.". Why is this step needed?
– Geek
Feb 11 '14 at 15:21
5
what does the first '.' do in . ~/.bashrc ?
– Zen
Jul 16 '14 at 4:13
18
@Geek @Zen "Execute" was not the correct term. The dot is equivalent tosource
. With. ~/.bashrc
, you source your bash. Executing the file would start a child process, execute the commands in this process, then return. All that is done in the child process has no effect on the parent process (the bash from which you executed). Instead, sourcing (with the dot) acts exactly as if you wrote the content of the file in the terminal. This is what you want..bashrc
is sourced everytime you start a bash. If you make changes, they won't apply until you start a new bash or source manually.
– Gauthier
Oct 3 '14 at 9:19
@ButtleButkus - might want to change just one user's preferences rather than the whole system. In Ubuntu the system-wide .bashrc file is /etc/bash.bashrc
– WillC
Oct 26 '16 at 3:30
This only works partially. I need to execute. ~/.bashrc
every time I open the terminal. Using OS X EI Captian (v10.11.6).
– Shubham A.
Apr 8 '17 at 16:25
|
show 2 more comments
There are lot of ways to create alias. The most used ways are :
Add aliases directly in your
~/.bashrc
file
For example : append these line to
~/.bashrc
file
alias ll='ls -l'
alias rm='rm -i'
Next time (after you have logged out/in, or done
. ~/.bashrc
) when you typerm
therm -i
command will be executed.
The second method lets you make a separate aliases file, so you won't have to put them in
.bashrc
, but to a file of your choice. First, edit your~/.bashrc
file and add or uncomment the following lines, if it is not already
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
Save it and close the file. After that, all you have to do is create a
~/.bash_aliases
file and add your aliases there, with the same format specified in the first method.
Contents of my
~/.bash_aliases
file:
alias cs='cd;ls'
63
+1 for using~/.bash_aliases
.
– ændrük
Jun 10 '11 at 5:48
Yep, using another file for aliases is much more clean, also portable between distros. I do use that file to because some alias are not enough and a function is needed. So it's more much clean if you use a file for that task instead. I have another alias ->alias aliases="xdg-open ~/.bash_aliases && source ~/.bash_aliases"
, so the alias became avail at saving, and if you make some mistake it will advert you.
– erm3nda
Apr 27 '17 at 12:08
somehow after I added the alias such asalias ls='ls -althr'
, some of the flags given would not take effect, in this case the -t flag didn't take effect. do you know why?
– Sajuuk
Mar 22 '18 at 8:43
2
By default~/.bashrc
contains inclusion for~/.bash_aliases
, no need to edit it.
– Jaakko
May 14 '18 at 9:49
Not always ~/.bashrc contains inclusion for ~/.bash_aliases as was in my case with Ubuntu terminal Windows 10 this solution came very handy.
– Jose
May 30 '18 at 18:10
add a comment |
The problem is that you are trying to execute a non executable file:
You can check this with:
ls -la ~/.bashrc
-rw-r--r-- 1 username username 3596 2010-08-05 17:17 /home/pt001424/.bashrc
Note there is no "x - executable" letter on the first column (file permissions).
Profile files are not executable files, instead of executing them you load them with:
source /home/bakhtiyor/.bashrc
or
. /home/bakhtiyor/.bashrc
add a comment |
It sounds to me like your only problem is simply trying to execute .bashrc when it is not executable. But this isn't the correct way to do it; whenever you make a change to this file, you should "execute" it by the command:
source ~/.bashrc
Otherwise, it will simply create a new shell, execute the file in the new shell's environment, then discard that environment when it exits, thereby losing your change. By sourcing the script, it executes within the current shell, so it will remain in effect.
I'm assuming the second error was because bash_aliases does not exist. It is not required, just recommended to keep your changes separate and organized. It is only used if it exists, and you can see the test for it in .bashrc:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
This says that if the file ~/.bash_aliases exists, then run it.
~/.bash_aliases
is better. askubuntu.com/questions/194111/…
– tomByrer
Jul 8 '14 at 2:39
1
using thesource
command made my aliases work. +1
– dspacejs
Dec 2 '15 at 10:00
4
Just for completeness: The initial dot on the line. ~/.bash_aliases
has the same meaning as the shell built-in commandsource
. I find this to be the correct answer, explaining what’s going on.
– Melebius
Feb 9 '17 at 7:46
add a comment |
echo "alias vps='ssh -X user@example.com'" >> ~/.bashrc
This is an example I was looking for, a way to type a few letters at the terminal ("vps") to remotely log in to a server and enable X11 forwarding so I can run gui apps like "gedit" over the network.
Whatever the command / aliased command, this way with the echo statement, quotation marks, and the symbol for appending the output of a command to a file (>>) works for me. Just replace my command for the alias command you need and enter it into your terminal.
The quoting here is slightly tricky. In this example, using double quotes is unproblematic, but if the text within the quotes contains dollar signs, backslashes, etc, you will need to understand how the shell processes them inside double quotes. You can switch to single quotes on the outside and double quotes on the inside, but you then still need to understand how Bash processes the double quotes in the alias definition.
– tripleee
Nov 30 '15 at 6:04
add a comment |
if you are using ruby, you can install aka using rubygem.
gem install aka2
usage
aka generate hello="echo helloworld" #add an alias
aka g hello="echo helloworld" #add alias for lazy people
aka destroy hello #remove alias
aka d hello #remove alias for lazy people
the rubygem will auto-source your dot file so that you don't need to. Check it out.
add a comment |
I wrote this helpful function to quickly create a new alias, and then write the alias definition to ~/.bash_aliases
(if it exists) or ~/.bashrc
.
TIP: Ensure ~/.bash_aliases
exists & is executed in ~/.bashrc
.
# -----------------------------------
# Create a new permanent bash alias
#
# @param $1 - name
# @param $2 - definition
# -----------------------------------
new-alias () {
if [ -z "$1" ]; then
echo "alias name:" && read NAME
else
NAME=$1
fi
if alias $NAME 2 > /dev/null > /dev/null; then
echo "alias $NAME already exists - continue [y/n]?" && read YN
case $YN in
[Yy]* ) echo "okay, let's proceed.";;
[Nn]* ) return;;
* ) echo "invalid response." && return;;
esac
fi
if [ -z "$2" ]; then
echo "alias definition:" && read DEFINTION
else
DEFINTION="$2"
fi
if [ -f ~/.bash_aliases ]; then
echo "alias $NAME="$DEFINTION"" >> ~/.bash_aliases
else
echo "alias $NAME="$DEFINTION"" >> ~/.bashrc
fi
alias $NAME="$DEFINTION"
}
1
Good idea, but careless using of this function may lead to trashing.bashrc
with multiple instances ofalias
command. Your function definitely needs to implement some checkups to avoid such cluttering.
– Troublemaker-DV
Mar 31 '16 at 1:04
This is a valid point. Do you have a suggested workaround you are willing to share @Troublemaker-DV?
– blizzrdof77
Dec 26 '18 at 21:02
Unsure if it is still actual 2 1/2yo later, but... At 1st I would grep the RC for alias commands to check, if this alias was already entered there to avoid duplicates. Your check for an existence of an alias is not enough, because the RC could be already "contaminated" with multiple aliases of same name.
– Troublemaker-DV
Dec 27 '18 at 6:51
add a comment |
I would suggest using /etc/bash.bashrc
You can add line at the end of that file.
alias ok="ping google.com"
After putting the aliases per line you have to reboot or relogin.
7
"I would suggest .." and why would you suggest that?
– muru
Aug 9 '15 at 4:17
2
You should not be messing with the system file unless you specifically want to install a system-wide setting for all users. On a personal system, the difference is marginal, but then messing with system files is more complicated down the road, so you should probably still prefer your own personal dot files for personal preferences (and that makes it easier to copy the settings somewhere else in the future, too).
– tripleee
Nov 30 '15 at 6:06
Reboot? That's really terrible advice, DON'T do that, especially whensource /etc/bash.bashrc
does all you want in this example. But should use~/.bashrc
or~/.bash_aliases
instead
– Xen2050
Jan 18 '18 at 12:50
@Xen2050 , I suggested reboot to show it works after reboot/relogin. By the way, even another clean terminal window will also work.
– Fahad Ahammed
Jan 19 '18 at 15:52
1
You can upgrade to a new kernel without rebooting, this ain't old windows ;-)
– Xen2050
Jan 19 '18 at 15:54
add a comment |
As I recall, bashrc
has, or had, a line suggesting to not use it for aliases directly. The solution is to use an external file(s). The foo
and bar
aliases have been added, but to add baz
the bashrc
file must be "sourced" (or, just open a new terminal). Example as:
thufir@dur:~$
thufir@dur:~$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '''s/^s*[0-9]+s*//;s/[;&|]s*alert$//''')"'
alias bar='echo foo'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias foo='echo foo'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
thufir@dur:~$
thufir@dur:~$ cat .bash_aliases
alias foo='echo foo'
alias bar='echo foo'
alias baz='echo baz'
thufir@dur:~$
thufir@dur:~$ source .bashrc
thufir@dur:~$
thufir@dur:~$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '''s/^s*[0-9]+s*//;s/[;&|]s*alert$//''')"'
alias bar='echo foo'
alias baz='echo baz'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias foo='echo foo'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
thufir@dur:~$
thufir@dur:~$ baz
baz
thufir@dur:~$
now the baz
alias works. I only just now realized that a previous answer had mentioned this technique, but they had buried the lede.
add a comment |
protected by heemayl Aug 14 '15 at 18:22
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
To create an alias permanently add the alias to your .bashrc
file
gedit ~/.bashrc
And then add your alias at the bottom.
Now execute . ~/.bashrc
in your terminal (there should be a space between the .
and ~/.bashrc
.
Now you can check your alias.
4
@karthick87 you wrote "Now execute . ~/.bashrc in your terminal (there should be a gap between the . and ~/.bashrc.". Why is this step needed?
– Geek
Feb 11 '14 at 15:21
5
what does the first '.' do in . ~/.bashrc ?
– Zen
Jul 16 '14 at 4:13
18
@Geek @Zen "Execute" was not the correct term. The dot is equivalent tosource
. With. ~/.bashrc
, you source your bash. Executing the file would start a child process, execute the commands in this process, then return. All that is done in the child process has no effect on the parent process (the bash from which you executed). Instead, sourcing (with the dot) acts exactly as if you wrote the content of the file in the terminal. This is what you want..bashrc
is sourced everytime you start a bash. If you make changes, they won't apply until you start a new bash or source manually.
– Gauthier
Oct 3 '14 at 9:19
@ButtleButkus - might want to change just one user's preferences rather than the whole system. In Ubuntu the system-wide .bashrc file is /etc/bash.bashrc
– WillC
Oct 26 '16 at 3:30
This only works partially. I need to execute. ~/.bashrc
every time I open the terminal. Using OS X EI Captian (v10.11.6).
– Shubham A.
Apr 8 '17 at 16:25
|
show 2 more comments
To create an alias permanently add the alias to your .bashrc
file
gedit ~/.bashrc
And then add your alias at the bottom.
Now execute . ~/.bashrc
in your terminal (there should be a space between the .
and ~/.bashrc
.
Now you can check your alias.
4
@karthick87 you wrote "Now execute . ~/.bashrc in your terminal (there should be a gap between the . and ~/.bashrc.". Why is this step needed?
– Geek
Feb 11 '14 at 15:21
5
what does the first '.' do in . ~/.bashrc ?
– Zen
Jul 16 '14 at 4:13
18
@Geek @Zen "Execute" was not the correct term. The dot is equivalent tosource
. With. ~/.bashrc
, you source your bash. Executing the file would start a child process, execute the commands in this process, then return. All that is done in the child process has no effect on the parent process (the bash from which you executed). Instead, sourcing (with the dot) acts exactly as if you wrote the content of the file in the terminal. This is what you want..bashrc
is sourced everytime you start a bash. If you make changes, they won't apply until you start a new bash or source manually.
– Gauthier
Oct 3 '14 at 9:19
@ButtleButkus - might want to change just one user's preferences rather than the whole system. In Ubuntu the system-wide .bashrc file is /etc/bash.bashrc
– WillC
Oct 26 '16 at 3:30
This only works partially. I need to execute. ~/.bashrc
every time I open the terminal. Using OS X EI Captian (v10.11.6).
– Shubham A.
Apr 8 '17 at 16:25
|
show 2 more comments
To create an alias permanently add the alias to your .bashrc
file
gedit ~/.bashrc
And then add your alias at the bottom.
Now execute . ~/.bashrc
in your terminal (there should be a space between the .
and ~/.bashrc
.
Now you can check your alias.
To create an alias permanently add the alias to your .bashrc
file
gedit ~/.bashrc
And then add your alias at the bottom.
Now execute . ~/.bashrc
in your terminal (there should be a space between the .
and ~/.bashrc
.
Now you can check your alias.
edited Oct 11 '16 at 17:36
ljk
54
54
answered Dec 15 '10 at 8:24
karthick87karthick87
47.9k53166217
47.9k53166217
4
@karthick87 you wrote "Now execute . ~/.bashrc in your terminal (there should be a gap between the . and ~/.bashrc.". Why is this step needed?
– Geek
Feb 11 '14 at 15:21
5
what does the first '.' do in . ~/.bashrc ?
– Zen
Jul 16 '14 at 4:13
18
@Geek @Zen "Execute" was not the correct term. The dot is equivalent tosource
. With. ~/.bashrc
, you source your bash. Executing the file would start a child process, execute the commands in this process, then return. All that is done in the child process has no effect on the parent process (the bash from which you executed). Instead, sourcing (with the dot) acts exactly as if you wrote the content of the file in the terminal. This is what you want..bashrc
is sourced everytime you start a bash. If you make changes, they won't apply until you start a new bash or source manually.
– Gauthier
Oct 3 '14 at 9:19
@ButtleButkus - might want to change just one user's preferences rather than the whole system. In Ubuntu the system-wide .bashrc file is /etc/bash.bashrc
– WillC
Oct 26 '16 at 3:30
This only works partially. I need to execute. ~/.bashrc
every time I open the terminal. Using OS X EI Captian (v10.11.6).
– Shubham A.
Apr 8 '17 at 16:25
|
show 2 more comments
4
@karthick87 you wrote "Now execute . ~/.bashrc in your terminal (there should be a gap between the . and ~/.bashrc.". Why is this step needed?
– Geek
Feb 11 '14 at 15:21
5
what does the first '.' do in . ~/.bashrc ?
– Zen
Jul 16 '14 at 4:13
18
@Geek @Zen "Execute" was not the correct term. The dot is equivalent tosource
. With. ~/.bashrc
, you source your bash. Executing the file would start a child process, execute the commands in this process, then return. All that is done in the child process has no effect on the parent process (the bash from which you executed). Instead, sourcing (with the dot) acts exactly as if you wrote the content of the file in the terminal. This is what you want..bashrc
is sourced everytime you start a bash. If you make changes, they won't apply until you start a new bash or source manually.
– Gauthier
Oct 3 '14 at 9:19
@ButtleButkus - might want to change just one user's preferences rather than the whole system. In Ubuntu the system-wide .bashrc file is /etc/bash.bashrc
– WillC
Oct 26 '16 at 3:30
This only works partially. I need to execute. ~/.bashrc
every time I open the terminal. Using OS X EI Captian (v10.11.6).
– Shubham A.
Apr 8 '17 at 16:25
4
4
@karthick87 you wrote "Now execute . ~/.bashrc in your terminal (there should be a gap between the . and ~/.bashrc.". Why is this step needed?
– Geek
Feb 11 '14 at 15:21
@karthick87 you wrote "Now execute . ~/.bashrc in your terminal (there should be a gap between the . and ~/.bashrc.". Why is this step needed?
– Geek
Feb 11 '14 at 15:21
5
5
what does the first '.' do in . ~/.bashrc ?
– Zen
Jul 16 '14 at 4:13
what does the first '.' do in . ~/.bashrc ?
– Zen
Jul 16 '14 at 4:13
18
18
@Geek @Zen "Execute" was not the correct term. The dot is equivalent to
source
. With . ~/.bashrc
, you source your bash. Executing the file would start a child process, execute the commands in this process, then return. All that is done in the child process has no effect on the parent process (the bash from which you executed). Instead, sourcing (with the dot) acts exactly as if you wrote the content of the file in the terminal. This is what you want. .bashrc
is sourced everytime you start a bash. If you make changes, they won't apply until you start a new bash or source manually.– Gauthier
Oct 3 '14 at 9:19
@Geek @Zen "Execute" was not the correct term. The dot is equivalent to
source
. With . ~/.bashrc
, you source your bash. Executing the file would start a child process, execute the commands in this process, then return. All that is done in the child process has no effect on the parent process (the bash from which you executed). Instead, sourcing (with the dot) acts exactly as if you wrote the content of the file in the terminal. This is what you want. .bashrc
is sourced everytime you start a bash. If you make changes, they won't apply until you start a new bash or source manually.– Gauthier
Oct 3 '14 at 9:19
@ButtleButkus - might want to change just one user's preferences rather than the whole system. In Ubuntu the system-wide .bashrc file is /etc/bash.bashrc
– WillC
Oct 26 '16 at 3:30
@ButtleButkus - might want to change just one user's preferences rather than the whole system. In Ubuntu the system-wide .bashrc file is /etc/bash.bashrc
– WillC
Oct 26 '16 at 3:30
This only works partially. I need to execute
. ~/.bashrc
every time I open the terminal. Using OS X EI Captian (v10.11.6).– Shubham A.
Apr 8 '17 at 16:25
This only works partially. I need to execute
. ~/.bashrc
every time I open the terminal. Using OS X EI Captian (v10.11.6).– Shubham A.
Apr 8 '17 at 16:25
|
show 2 more comments
There are lot of ways to create alias. The most used ways are :
Add aliases directly in your
~/.bashrc
file
For example : append these line to
~/.bashrc
file
alias ll='ls -l'
alias rm='rm -i'
Next time (after you have logged out/in, or done
. ~/.bashrc
) when you typerm
therm -i
command will be executed.
The second method lets you make a separate aliases file, so you won't have to put them in
.bashrc
, but to a file of your choice. First, edit your~/.bashrc
file and add or uncomment the following lines, if it is not already
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
Save it and close the file. After that, all you have to do is create a
~/.bash_aliases
file and add your aliases there, with the same format specified in the first method.
Contents of my
~/.bash_aliases
file:
alias cs='cd;ls'
63
+1 for using~/.bash_aliases
.
– ændrük
Jun 10 '11 at 5:48
Yep, using another file for aliases is much more clean, also portable between distros. I do use that file to because some alias are not enough and a function is needed. So it's more much clean if you use a file for that task instead. I have another alias ->alias aliases="xdg-open ~/.bash_aliases && source ~/.bash_aliases"
, so the alias became avail at saving, and if you make some mistake it will advert you.
– erm3nda
Apr 27 '17 at 12:08
somehow after I added the alias such asalias ls='ls -althr'
, some of the flags given would not take effect, in this case the -t flag didn't take effect. do you know why?
– Sajuuk
Mar 22 '18 at 8:43
2
By default~/.bashrc
contains inclusion for~/.bash_aliases
, no need to edit it.
– Jaakko
May 14 '18 at 9:49
Not always ~/.bashrc contains inclusion for ~/.bash_aliases as was in my case with Ubuntu terminal Windows 10 this solution came very handy.
– Jose
May 30 '18 at 18:10
add a comment |
There are lot of ways to create alias. The most used ways are :
Add aliases directly in your
~/.bashrc
file
For example : append these line to
~/.bashrc
file
alias ll='ls -l'
alias rm='rm -i'
Next time (after you have logged out/in, or done
. ~/.bashrc
) when you typerm
therm -i
command will be executed.
The second method lets you make a separate aliases file, so you won't have to put them in
.bashrc
, but to a file of your choice. First, edit your~/.bashrc
file and add or uncomment the following lines, if it is not already
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
Save it and close the file. After that, all you have to do is create a
~/.bash_aliases
file and add your aliases there, with the same format specified in the first method.
Contents of my
~/.bash_aliases
file:
alias cs='cd;ls'
63
+1 for using~/.bash_aliases
.
– ændrük
Jun 10 '11 at 5:48
Yep, using another file for aliases is much more clean, also portable between distros. I do use that file to because some alias are not enough and a function is needed. So it's more much clean if you use a file for that task instead. I have another alias ->alias aliases="xdg-open ~/.bash_aliases && source ~/.bash_aliases"
, so the alias became avail at saving, and if you make some mistake it will advert you.
– erm3nda
Apr 27 '17 at 12:08
somehow after I added the alias such asalias ls='ls -althr'
, some of the flags given would not take effect, in this case the -t flag didn't take effect. do you know why?
– Sajuuk
Mar 22 '18 at 8:43
2
By default~/.bashrc
contains inclusion for~/.bash_aliases
, no need to edit it.
– Jaakko
May 14 '18 at 9:49
Not always ~/.bashrc contains inclusion for ~/.bash_aliases as was in my case with Ubuntu terminal Windows 10 this solution came very handy.
– Jose
May 30 '18 at 18:10
add a comment |
There are lot of ways to create alias. The most used ways are :
Add aliases directly in your
~/.bashrc
file
For example : append these line to
~/.bashrc
file
alias ll='ls -l'
alias rm='rm -i'
Next time (after you have logged out/in, or done
. ~/.bashrc
) when you typerm
therm -i
command will be executed.
The second method lets you make a separate aliases file, so you won't have to put them in
.bashrc
, but to a file of your choice. First, edit your~/.bashrc
file and add or uncomment the following lines, if it is not already
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
Save it and close the file. After that, all you have to do is create a
~/.bash_aliases
file and add your aliases there, with the same format specified in the first method.
Contents of my
~/.bash_aliases
file:
alias cs='cd;ls'
There are lot of ways to create alias. The most used ways are :
Add aliases directly in your
~/.bashrc
file
For example : append these line to
~/.bashrc
file
alias ll='ls -l'
alias rm='rm -i'
Next time (after you have logged out/in, or done
. ~/.bashrc
) when you typerm
therm -i
command will be executed.
The second method lets you make a separate aliases file, so you won't have to put them in
.bashrc
, but to a file of your choice. First, edit your~/.bashrc
file and add or uncomment the following lines, if it is not already
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
Save it and close the file. After that, all you have to do is create a
~/.bash_aliases
file and add your aliases there, with the same format specified in the first method.
Contents of my
~/.bash_aliases
file:
alias cs='cd;ls'
edited Jan 30 '16 at 14:57
waltinator
22k74169
22k74169
answered Dec 15 '10 at 8:21
aneeshepaneeshep
22k115574
22k115574
63
+1 for using~/.bash_aliases
.
– ændrük
Jun 10 '11 at 5:48
Yep, using another file for aliases is much more clean, also portable between distros. I do use that file to because some alias are not enough and a function is needed. So it's more much clean if you use a file for that task instead. I have another alias ->alias aliases="xdg-open ~/.bash_aliases && source ~/.bash_aliases"
, so the alias became avail at saving, and if you make some mistake it will advert you.
– erm3nda
Apr 27 '17 at 12:08
somehow after I added the alias such asalias ls='ls -althr'
, some of the flags given would not take effect, in this case the -t flag didn't take effect. do you know why?
– Sajuuk
Mar 22 '18 at 8:43
2
By default~/.bashrc
contains inclusion for~/.bash_aliases
, no need to edit it.
– Jaakko
May 14 '18 at 9:49
Not always ~/.bashrc contains inclusion for ~/.bash_aliases as was in my case with Ubuntu terminal Windows 10 this solution came very handy.
– Jose
May 30 '18 at 18:10
add a comment |
63
+1 for using~/.bash_aliases
.
– ændrük
Jun 10 '11 at 5:48
Yep, using another file for aliases is much more clean, also portable between distros. I do use that file to because some alias are not enough and a function is needed. So it's more much clean if you use a file for that task instead. I have another alias ->alias aliases="xdg-open ~/.bash_aliases && source ~/.bash_aliases"
, so the alias became avail at saving, and if you make some mistake it will advert you.
– erm3nda
Apr 27 '17 at 12:08
somehow after I added the alias such asalias ls='ls -althr'
, some of the flags given would not take effect, in this case the -t flag didn't take effect. do you know why?
– Sajuuk
Mar 22 '18 at 8:43
2
By default~/.bashrc
contains inclusion for~/.bash_aliases
, no need to edit it.
– Jaakko
May 14 '18 at 9:49
Not always ~/.bashrc contains inclusion for ~/.bash_aliases as was in my case with Ubuntu terminal Windows 10 this solution came very handy.
– Jose
May 30 '18 at 18:10
63
63
+1 for using
~/.bash_aliases
.– ændrük
Jun 10 '11 at 5:48
+1 for using
~/.bash_aliases
.– ændrük
Jun 10 '11 at 5:48
Yep, using another file for aliases is much more clean, also portable between distros. I do use that file to because some alias are not enough and a function is needed. So it's more much clean if you use a file for that task instead. I have another alias ->
alias aliases="xdg-open ~/.bash_aliases && source ~/.bash_aliases"
, so the alias became avail at saving, and if you make some mistake it will advert you.– erm3nda
Apr 27 '17 at 12:08
Yep, using another file for aliases is much more clean, also portable between distros. I do use that file to because some alias are not enough and a function is needed. So it's more much clean if you use a file for that task instead. I have another alias ->
alias aliases="xdg-open ~/.bash_aliases && source ~/.bash_aliases"
, so the alias became avail at saving, and if you make some mistake it will advert you.– erm3nda
Apr 27 '17 at 12:08
somehow after I added the alias such as
alias ls='ls -althr'
, some of the flags given would not take effect, in this case the -t flag didn't take effect. do you know why?– Sajuuk
Mar 22 '18 at 8:43
somehow after I added the alias such as
alias ls='ls -althr'
, some of the flags given would not take effect, in this case the -t flag didn't take effect. do you know why?– Sajuuk
Mar 22 '18 at 8:43
2
2
By default
~/.bashrc
contains inclusion for ~/.bash_aliases
, no need to edit it.– Jaakko
May 14 '18 at 9:49
By default
~/.bashrc
contains inclusion for ~/.bash_aliases
, no need to edit it.– Jaakko
May 14 '18 at 9:49
Not always ~/.bashrc contains inclusion for ~/.bash_aliases as was in my case with Ubuntu terminal Windows 10 this solution came very handy.
– Jose
May 30 '18 at 18:10
Not always ~/.bashrc contains inclusion for ~/.bash_aliases as was in my case with Ubuntu terminal Windows 10 this solution came very handy.
– Jose
May 30 '18 at 18:10
add a comment |
The problem is that you are trying to execute a non executable file:
You can check this with:
ls -la ~/.bashrc
-rw-r--r-- 1 username username 3596 2010-08-05 17:17 /home/pt001424/.bashrc
Note there is no "x - executable" letter on the first column (file permissions).
Profile files are not executable files, instead of executing them you load them with:
source /home/bakhtiyor/.bashrc
or
. /home/bakhtiyor/.bashrc
add a comment |
The problem is that you are trying to execute a non executable file:
You can check this with:
ls -la ~/.bashrc
-rw-r--r-- 1 username username 3596 2010-08-05 17:17 /home/pt001424/.bashrc
Note there is no "x - executable" letter on the first column (file permissions).
Profile files are not executable files, instead of executing them you load them with:
source /home/bakhtiyor/.bashrc
or
. /home/bakhtiyor/.bashrc
add a comment |
The problem is that you are trying to execute a non executable file:
You can check this with:
ls -la ~/.bashrc
-rw-r--r-- 1 username username 3596 2010-08-05 17:17 /home/pt001424/.bashrc
Note there is no "x - executable" letter on the first column (file permissions).
Profile files are not executable files, instead of executing them you load them with:
source /home/bakhtiyor/.bashrc
or
. /home/bakhtiyor/.bashrc
The problem is that you are trying to execute a non executable file:
You can check this with:
ls -la ~/.bashrc
-rw-r--r-- 1 username username 3596 2010-08-05 17:17 /home/pt001424/.bashrc
Note there is no "x - executable" letter on the first column (file permissions).
Profile files are not executable files, instead of executing them you load them with:
source /home/bakhtiyor/.bashrc
or
. /home/bakhtiyor/.bashrc
answered Dec 15 '10 at 10:24
João PintoJoão Pinto
14.7k34660
14.7k34660
add a comment |
add a comment |
It sounds to me like your only problem is simply trying to execute .bashrc when it is not executable. But this isn't the correct way to do it; whenever you make a change to this file, you should "execute" it by the command:
source ~/.bashrc
Otherwise, it will simply create a new shell, execute the file in the new shell's environment, then discard that environment when it exits, thereby losing your change. By sourcing the script, it executes within the current shell, so it will remain in effect.
I'm assuming the second error was because bash_aliases does not exist. It is not required, just recommended to keep your changes separate and organized. It is only used if it exists, and you can see the test for it in .bashrc:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
This says that if the file ~/.bash_aliases exists, then run it.
~/.bash_aliases
is better. askubuntu.com/questions/194111/…
– tomByrer
Jul 8 '14 at 2:39
1
using thesource
command made my aliases work. +1
– dspacejs
Dec 2 '15 at 10:00
4
Just for completeness: The initial dot on the line. ~/.bash_aliases
has the same meaning as the shell built-in commandsource
. I find this to be the correct answer, explaining what’s going on.
– Melebius
Feb 9 '17 at 7:46
add a comment |
It sounds to me like your only problem is simply trying to execute .bashrc when it is not executable. But this isn't the correct way to do it; whenever you make a change to this file, you should "execute" it by the command:
source ~/.bashrc
Otherwise, it will simply create a new shell, execute the file in the new shell's environment, then discard that environment when it exits, thereby losing your change. By sourcing the script, it executes within the current shell, so it will remain in effect.
I'm assuming the second error was because bash_aliases does not exist. It is not required, just recommended to keep your changes separate and organized. It is only used if it exists, and you can see the test for it in .bashrc:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
This says that if the file ~/.bash_aliases exists, then run it.
~/.bash_aliases
is better. askubuntu.com/questions/194111/…
– tomByrer
Jul 8 '14 at 2:39
1
using thesource
command made my aliases work. +1
– dspacejs
Dec 2 '15 at 10:00
4
Just for completeness: The initial dot on the line. ~/.bash_aliases
has the same meaning as the shell built-in commandsource
. I find this to be the correct answer, explaining what’s going on.
– Melebius
Feb 9 '17 at 7:46
add a comment |
It sounds to me like your only problem is simply trying to execute .bashrc when it is not executable. But this isn't the correct way to do it; whenever you make a change to this file, you should "execute" it by the command:
source ~/.bashrc
Otherwise, it will simply create a new shell, execute the file in the new shell's environment, then discard that environment when it exits, thereby losing your change. By sourcing the script, it executes within the current shell, so it will remain in effect.
I'm assuming the second error was because bash_aliases does not exist. It is not required, just recommended to keep your changes separate and organized. It is only used if it exists, and you can see the test for it in .bashrc:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
This says that if the file ~/.bash_aliases exists, then run it.
It sounds to me like your only problem is simply trying to execute .bashrc when it is not executable. But this isn't the correct way to do it; whenever you make a change to this file, you should "execute" it by the command:
source ~/.bashrc
Otherwise, it will simply create a new shell, execute the file in the new shell's environment, then discard that environment when it exits, thereby losing your change. By sourcing the script, it executes within the current shell, so it will remain in effect.
I'm assuming the second error was because bash_aliases does not exist. It is not required, just recommended to keep your changes separate and organized. It is only used if it exists, and you can see the test for it in .bashrc:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
This says that if the file ~/.bash_aliases exists, then run it.
answered Jan 16 '12 at 20:29
Marty FriedMarty Fried
13.4k53847
13.4k53847
~/.bash_aliases
is better. askubuntu.com/questions/194111/…
– tomByrer
Jul 8 '14 at 2:39
1
using thesource
command made my aliases work. +1
– dspacejs
Dec 2 '15 at 10:00
4
Just for completeness: The initial dot on the line. ~/.bash_aliases
has the same meaning as the shell built-in commandsource
. I find this to be the correct answer, explaining what’s going on.
– Melebius
Feb 9 '17 at 7:46
add a comment |
~/.bash_aliases
is better. askubuntu.com/questions/194111/…
– tomByrer
Jul 8 '14 at 2:39
1
using thesource
command made my aliases work. +1
– dspacejs
Dec 2 '15 at 10:00
4
Just for completeness: The initial dot on the line. ~/.bash_aliases
has the same meaning as the shell built-in commandsource
. I find this to be the correct answer, explaining what’s going on.
– Melebius
Feb 9 '17 at 7:46
~/.bash_aliases
is better. askubuntu.com/questions/194111/…– tomByrer
Jul 8 '14 at 2:39
~/.bash_aliases
is better. askubuntu.com/questions/194111/…– tomByrer
Jul 8 '14 at 2:39
1
1
using the
source
command made my aliases work. +1– dspacejs
Dec 2 '15 at 10:00
using the
source
command made my aliases work. +1– dspacejs
Dec 2 '15 at 10:00
4
4
Just for completeness: The initial dot on the line
. ~/.bash_aliases
has the same meaning as the shell built-in command source
. I find this to be the correct answer, explaining what’s going on.– Melebius
Feb 9 '17 at 7:46
Just for completeness: The initial dot on the line
. ~/.bash_aliases
has the same meaning as the shell built-in command source
. I find this to be the correct answer, explaining what’s going on.– Melebius
Feb 9 '17 at 7:46
add a comment |
echo "alias vps='ssh -X user@example.com'" >> ~/.bashrc
This is an example I was looking for, a way to type a few letters at the terminal ("vps") to remotely log in to a server and enable X11 forwarding so I can run gui apps like "gedit" over the network.
Whatever the command / aliased command, this way with the echo statement, quotation marks, and the symbol for appending the output of a command to a file (>>) works for me. Just replace my command for the alias command you need and enter it into your terminal.
The quoting here is slightly tricky. In this example, using double quotes is unproblematic, but if the text within the quotes contains dollar signs, backslashes, etc, you will need to understand how the shell processes them inside double quotes. You can switch to single quotes on the outside and double quotes on the inside, but you then still need to understand how Bash processes the double quotes in the alias definition.
– tripleee
Nov 30 '15 at 6:04
add a comment |
echo "alias vps='ssh -X user@example.com'" >> ~/.bashrc
This is an example I was looking for, a way to type a few letters at the terminal ("vps") to remotely log in to a server and enable X11 forwarding so I can run gui apps like "gedit" over the network.
Whatever the command / aliased command, this way with the echo statement, quotation marks, and the symbol for appending the output of a command to a file (>>) works for me. Just replace my command for the alias command you need and enter it into your terminal.
The quoting here is slightly tricky. In this example, using double quotes is unproblematic, but if the text within the quotes contains dollar signs, backslashes, etc, you will need to understand how the shell processes them inside double quotes. You can switch to single quotes on the outside and double quotes on the inside, but you then still need to understand how Bash processes the double quotes in the alias definition.
– tripleee
Nov 30 '15 at 6:04
add a comment |
echo "alias vps='ssh -X user@example.com'" >> ~/.bashrc
This is an example I was looking for, a way to type a few letters at the terminal ("vps") to remotely log in to a server and enable X11 forwarding so I can run gui apps like "gedit" over the network.
Whatever the command / aliased command, this way with the echo statement, quotation marks, and the symbol for appending the output of a command to a file (>>) works for me. Just replace my command for the alias command you need and enter it into your terminal.
echo "alias vps='ssh -X user@example.com'" >> ~/.bashrc
This is an example I was looking for, a way to type a few letters at the terminal ("vps") to remotely log in to a server and enable X11 forwarding so I can run gui apps like "gedit" over the network.
Whatever the command / aliased command, this way with the echo statement, quotation marks, and the symbol for appending the output of a command to a file (>>) works for me. Just replace my command for the alias command you need and enter it into your terminal.
edited Jun 15 '13 at 11:45
answered Jun 15 '13 at 11:39
user80638user80638
8124
8124
The quoting here is slightly tricky. In this example, using double quotes is unproblematic, but if the text within the quotes contains dollar signs, backslashes, etc, you will need to understand how the shell processes them inside double quotes. You can switch to single quotes on the outside and double quotes on the inside, but you then still need to understand how Bash processes the double quotes in the alias definition.
– tripleee
Nov 30 '15 at 6:04
add a comment |
The quoting here is slightly tricky. In this example, using double quotes is unproblematic, but if the text within the quotes contains dollar signs, backslashes, etc, you will need to understand how the shell processes them inside double quotes. You can switch to single quotes on the outside and double quotes on the inside, but you then still need to understand how Bash processes the double quotes in the alias definition.
– tripleee
Nov 30 '15 at 6:04
The quoting here is slightly tricky. In this example, using double quotes is unproblematic, but if the text within the quotes contains dollar signs, backslashes, etc, you will need to understand how the shell processes them inside double quotes. You can switch to single quotes on the outside and double quotes on the inside, but you then still need to understand how Bash processes the double quotes in the alias definition.
– tripleee
Nov 30 '15 at 6:04
The quoting here is slightly tricky. In this example, using double quotes is unproblematic, but if the text within the quotes contains dollar signs, backslashes, etc, you will need to understand how the shell processes them inside double quotes. You can switch to single quotes on the outside and double quotes on the inside, but you then still need to understand how Bash processes the double quotes in the alias definition.
– tripleee
Nov 30 '15 at 6:04
add a comment |
if you are using ruby, you can install aka using rubygem.
gem install aka2
usage
aka generate hello="echo helloworld" #add an alias
aka g hello="echo helloworld" #add alias for lazy people
aka destroy hello #remove alias
aka d hello #remove alias for lazy people
the rubygem will auto-source your dot file so that you don't need to. Check it out.
add a comment |
if you are using ruby, you can install aka using rubygem.
gem install aka2
usage
aka generate hello="echo helloworld" #add an alias
aka g hello="echo helloworld" #add alias for lazy people
aka destroy hello #remove alias
aka d hello #remove alias for lazy people
the rubygem will auto-source your dot file so that you don't need to. Check it out.
add a comment |
if you are using ruby, you can install aka using rubygem.
gem install aka2
usage
aka generate hello="echo helloworld" #add an alias
aka g hello="echo helloworld" #add alias for lazy people
aka destroy hello #remove alias
aka d hello #remove alias for lazy people
the rubygem will auto-source your dot file so that you don't need to. Check it out.
if you are using ruby, you can install aka using rubygem.
gem install aka2
usage
aka generate hello="echo helloworld" #add an alias
aka g hello="echo helloworld" #add alias for lazy people
aka destroy hello #remove alias
aka d hello #remove alias for lazy people
the rubygem will auto-source your dot file so that you don't need to. Check it out.
edited Oct 26 '15 at 16:19
answered Jan 21 '15 at 14:14
ytbryanytbryan
15113
15113
add a comment |
add a comment |
I wrote this helpful function to quickly create a new alias, and then write the alias definition to ~/.bash_aliases
(if it exists) or ~/.bashrc
.
TIP: Ensure ~/.bash_aliases
exists & is executed in ~/.bashrc
.
# -----------------------------------
# Create a new permanent bash alias
#
# @param $1 - name
# @param $2 - definition
# -----------------------------------
new-alias () {
if [ -z "$1" ]; then
echo "alias name:" && read NAME
else
NAME=$1
fi
if alias $NAME 2 > /dev/null > /dev/null; then
echo "alias $NAME already exists - continue [y/n]?" && read YN
case $YN in
[Yy]* ) echo "okay, let's proceed.";;
[Nn]* ) return;;
* ) echo "invalid response." && return;;
esac
fi
if [ -z "$2" ]; then
echo "alias definition:" && read DEFINTION
else
DEFINTION="$2"
fi
if [ -f ~/.bash_aliases ]; then
echo "alias $NAME="$DEFINTION"" >> ~/.bash_aliases
else
echo "alias $NAME="$DEFINTION"" >> ~/.bashrc
fi
alias $NAME="$DEFINTION"
}
1
Good idea, but careless using of this function may lead to trashing.bashrc
with multiple instances ofalias
command. Your function definitely needs to implement some checkups to avoid such cluttering.
– Troublemaker-DV
Mar 31 '16 at 1:04
This is a valid point. Do you have a suggested workaround you are willing to share @Troublemaker-DV?
– blizzrdof77
Dec 26 '18 at 21:02
Unsure if it is still actual 2 1/2yo later, but... At 1st I would grep the RC for alias commands to check, if this alias was already entered there to avoid duplicates. Your check for an existence of an alias is not enough, because the RC could be already "contaminated" with multiple aliases of same name.
– Troublemaker-DV
Dec 27 '18 at 6:51
add a comment |
I wrote this helpful function to quickly create a new alias, and then write the alias definition to ~/.bash_aliases
(if it exists) or ~/.bashrc
.
TIP: Ensure ~/.bash_aliases
exists & is executed in ~/.bashrc
.
# -----------------------------------
# Create a new permanent bash alias
#
# @param $1 - name
# @param $2 - definition
# -----------------------------------
new-alias () {
if [ -z "$1" ]; then
echo "alias name:" && read NAME
else
NAME=$1
fi
if alias $NAME 2 > /dev/null > /dev/null; then
echo "alias $NAME already exists - continue [y/n]?" && read YN
case $YN in
[Yy]* ) echo "okay, let's proceed.";;
[Nn]* ) return;;
* ) echo "invalid response." && return;;
esac
fi
if [ -z "$2" ]; then
echo "alias definition:" && read DEFINTION
else
DEFINTION="$2"
fi
if [ -f ~/.bash_aliases ]; then
echo "alias $NAME="$DEFINTION"" >> ~/.bash_aliases
else
echo "alias $NAME="$DEFINTION"" >> ~/.bashrc
fi
alias $NAME="$DEFINTION"
}
1
Good idea, but careless using of this function may lead to trashing.bashrc
with multiple instances ofalias
command. Your function definitely needs to implement some checkups to avoid such cluttering.
– Troublemaker-DV
Mar 31 '16 at 1:04
This is a valid point. Do you have a suggested workaround you are willing to share @Troublemaker-DV?
– blizzrdof77
Dec 26 '18 at 21:02
Unsure if it is still actual 2 1/2yo later, but... At 1st I would grep the RC for alias commands to check, if this alias was already entered there to avoid duplicates. Your check for an existence of an alias is not enough, because the RC could be already "contaminated" with multiple aliases of same name.
– Troublemaker-DV
Dec 27 '18 at 6:51
add a comment |
I wrote this helpful function to quickly create a new alias, and then write the alias definition to ~/.bash_aliases
(if it exists) or ~/.bashrc
.
TIP: Ensure ~/.bash_aliases
exists & is executed in ~/.bashrc
.
# -----------------------------------
# Create a new permanent bash alias
#
# @param $1 - name
# @param $2 - definition
# -----------------------------------
new-alias () {
if [ -z "$1" ]; then
echo "alias name:" && read NAME
else
NAME=$1
fi
if alias $NAME 2 > /dev/null > /dev/null; then
echo "alias $NAME already exists - continue [y/n]?" && read YN
case $YN in
[Yy]* ) echo "okay, let's proceed.";;
[Nn]* ) return;;
* ) echo "invalid response." && return;;
esac
fi
if [ -z "$2" ]; then
echo "alias definition:" && read DEFINTION
else
DEFINTION="$2"
fi
if [ -f ~/.bash_aliases ]; then
echo "alias $NAME="$DEFINTION"" >> ~/.bash_aliases
else
echo "alias $NAME="$DEFINTION"" >> ~/.bashrc
fi
alias $NAME="$DEFINTION"
}
I wrote this helpful function to quickly create a new alias, and then write the alias definition to ~/.bash_aliases
(if it exists) or ~/.bashrc
.
TIP: Ensure ~/.bash_aliases
exists & is executed in ~/.bashrc
.
# -----------------------------------
# Create a new permanent bash alias
#
# @param $1 - name
# @param $2 - definition
# -----------------------------------
new-alias () {
if [ -z "$1" ]; then
echo "alias name:" && read NAME
else
NAME=$1
fi
if alias $NAME 2 > /dev/null > /dev/null; then
echo "alias $NAME already exists - continue [y/n]?" && read YN
case $YN in
[Yy]* ) echo "okay, let's proceed.";;
[Nn]* ) return;;
* ) echo "invalid response." && return;;
esac
fi
if [ -z "$2" ]; then
echo "alias definition:" && read DEFINTION
else
DEFINTION="$2"
fi
if [ -f ~/.bash_aliases ]; then
echo "alias $NAME="$DEFINTION"" >> ~/.bash_aliases
else
echo "alias $NAME="$DEFINTION"" >> ~/.bashrc
fi
alias $NAME="$DEFINTION"
}
edited Dec 26 '18 at 21:34
answered Dec 17 '13 at 20:37
blizzrdof77blizzrdof77
5916
5916
1
Good idea, but careless using of this function may lead to trashing.bashrc
with multiple instances ofalias
command. Your function definitely needs to implement some checkups to avoid such cluttering.
– Troublemaker-DV
Mar 31 '16 at 1:04
This is a valid point. Do you have a suggested workaround you are willing to share @Troublemaker-DV?
– blizzrdof77
Dec 26 '18 at 21:02
Unsure if it is still actual 2 1/2yo later, but... At 1st I would grep the RC for alias commands to check, if this alias was already entered there to avoid duplicates. Your check for an existence of an alias is not enough, because the RC could be already "contaminated" with multiple aliases of same name.
– Troublemaker-DV
Dec 27 '18 at 6:51
add a comment |
1
Good idea, but careless using of this function may lead to trashing.bashrc
with multiple instances ofalias
command. Your function definitely needs to implement some checkups to avoid such cluttering.
– Troublemaker-DV
Mar 31 '16 at 1:04
This is a valid point. Do you have a suggested workaround you are willing to share @Troublemaker-DV?
– blizzrdof77
Dec 26 '18 at 21:02
Unsure if it is still actual 2 1/2yo later, but... At 1st I would grep the RC for alias commands to check, if this alias was already entered there to avoid duplicates. Your check for an existence of an alias is not enough, because the RC could be already "contaminated" with multiple aliases of same name.
– Troublemaker-DV
Dec 27 '18 at 6:51
1
1
Good idea, but careless using of this function may lead to trashing
.bashrc
with multiple instances of alias
command. Your function definitely needs to implement some checkups to avoid such cluttering.– Troublemaker-DV
Mar 31 '16 at 1:04
Good idea, but careless using of this function may lead to trashing
.bashrc
with multiple instances of alias
command. Your function definitely needs to implement some checkups to avoid such cluttering.– Troublemaker-DV
Mar 31 '16 at 1:04
This is a valid point. Do you have a suggested workaround you are willing to share @Troublemaker-DV?
– blizzrdof77
Dec 26 '18 at 21:02
This is a valid point. Do you have a suggested workaround you are willing to share @Troublemaker-DV?
– blizzrdof77
Dec 26 '18 at 21:02
Unsure if it is still actual 2 1/2yo later, but... At 1st I would grep the RC for alias commands to check, if this alias was already entered there to avoid duplicates. Your check for an existence of an alias is not enough, because the RC could be already "contaminated" with multiple aliases of same name.
– Troublemaker-DV
Dec 27 '18 at 6:51
Unsure if it is still actual 2 1/2yo later, but... At 1st I would grep the RC for alias commands to check, if this alias was already entered there to avoid duplicates. Your check for an existence of an alias is not enough, because the RC could be already "contaminated" with multiple aliases of same name.
– Troublemaker-DV
Dec 27 '18 at 6:51
add a comment |
I would suggest using /etc/bash.bashrc
You can add line at the end of that file.
alias ok="ping google.com"
After putting the aliases per line you have to reboot or relogin.
7
"I would suggest .." and why would you suggest that?
– muru
Aug 9 '15 at 4:17
2
You should not be messing with the system file unless you specifically want to install a system-wide setting for all users. On a personal system, the difference is marginal, but then messing with system files is more complicated down the road, so you should probably still prefer your own personal dot files for personal preferences (and that makes it easier to copy the settings somewhere else in the future, too).
– tripleee
Nov 30 '15 at 6:06
Reboot? That's really terrible advice, DON'T do that, especially whensource /etc/bash.bashrc
does all you want in this example. But should use~/.bashrc
or~/.bash_aliases
instead
– Xen2050
Jan 18 '18 at 12:50
@Xen2050 , I suggested reboot to show it works after reboot/relogin. By the way, even another clean terminal window will also work.
– Fahad Ahammed
Jan 19 '18 at 15:52
1
You can upgrade to a new kernel without rebooting, this ain't old windows ;-)
– Xen2050
Jan 19 '18 at 15:54
add a comment |
I would suggest using /etc/bash.bashrc
You can add line at the end of that file.
alias ok="ping google.com"
After putting the aliases per line you have to reboot or relogin.
7
"I would suggest .." and why would you suggest that?
– muru
Aug 9 '15 at 4:17
2
You should not be messing with the system file unless you specifically want to install a system-wide setting for all users. On a personal system, the difference is marginal, but then messing with system files is more complicated down the road, so you should probably still prefer your own personal dot files for personal preferences (and that makes it easier to copy the settings somewhere else in the future, too).
– tripleee
Nov 30 '15 at 6:06
Reboot? That's really terrible advice, DON'T do that, especially whensource /etc/bash.bashrc
does all you want in this example. But should use~/.bashrc
or~/.bash_aliases
instead
– Xen2050
Jan 18 '18 at 12:50
@Xen2050 , I suggested reboot to show it works after reboot/relogin. By the way, even another clean terminal window will also work.
– Fahad Ahammed
Jan 19 '18 at 15:52
1
You can upgrade to a new kernel without rebooting, this ain't old windows ;-)
– Xen2050
Jan 19 '18 at 15:54
add a comment |
I would suggest using /etc/bash.bashrc
You can add line at the end of that file.
alias ok="ping google.com"
After putting the aliases per line you have to reboot or relogin.
I would suggest using /etc/bash.bashrc
You can add line at the end of that file.
alias ok="ping google.com"
After putting the aliases per line you have to reboot or relogin.
edited Aug 9 '15 at 4:16
muru
1
1
answered Aug 9 '15 at 3:36
Fahad AhammedFahad Ahammed
3933518
3933518
7
"I would suggest .." and why would you suggest that?
– muru
Aug 9 '15 at 4:17
2
You should not be messing with the system file unless you specifically want to install a system-wide setting for all users. On a personal system, the difference is marginal, but then messing with system files is more complicated down the road, so you should probably still prefer your own personal dot files for personal preferences (and that makes it easier to copy the settings somewhere else in the future, too).
– tripleee
Nov 30 '15 at 6:06
Reboot? That's really terrible advice, DON'T do that, especially whensource /etc/bash.bashrc
does all you want in this example. But should use~/.bashrc
or~/.bash_aliases
instead
– Xen2050
Jan 18 '18 at 12:50
@Xen2050 , I suggested reboot to show it works after reboot/relogin. By the way, even another clean terminal window will also work.
– Fahad Ahammed
Jan 19 '18 at 15:52
1
You can upgrade to a new kernel without rebooting, this ain't old windows ;-)
– Xen2050
Jan 19 '18 at 15:54
add a comment |
7
"I would suggest .." and why would you suggest that?
– muru
Aug 9 '15 at 4:17
2
You should not be messing with the system file unless you specifically want to install a system-wide setting for all users. On a personal system, the difference is marginal, but then messing with system files is more complicated down the road, so you should probably still prefer your own personal dot files for personal preferences (and that makes it easier to copy the settings somewhere else in the future, too).
– tripleee
Nov 30 '15 at 6:06
Reboot? That's really terrible advice, DON'T do that, especially whensource /etc/bash.bashrc
does all you want in this example. But should use~/.bashrc
or~/.bash_aliases
instead
– Xen2050
Jan 18 '18 at 12:50
@Xen2050 , I suggested reboot to show it works after reboot/relogin. By the way, even another clean terminal window will also work.
– Fahad Ahammed
Jan 19 '18 at 15:52
1
You can upgrade to a new kernel without rebooting, this ain't old windows ;-)
– Xen2050
Jan 19 '18 at 15:54
7
7
"I would suggest .." and why would you suggest that?
– muru
Aug 9 '15 at 4:17
"I would suggest .." and why would you suggest that?
– muru
Aug 9 '15 at 4:17
2
2
You should not be messing with the system file unless you specifically want to install a system-wide setting for all users. On a personal system, the difference is marginal, but then messing with system files is more complicated down the road, so you should probably still prefer your own personal dot files for personal preferences (and that makes it easier to copy the settings somewhere else in the future, too).
– tripleee
Nov 30 '15 at 6:06
You should not be messing with the system file unless you specifically want to install a system-wide setting for all users. On a personal system, the difference is marginal, but then messing with system files is more complicated down the road, so you should probably still prefer your own personal dot files for personal preferences (and that makes it easier to copy the settings somewhere else in the future, too).
– tripleee
Nov 30 '15 at 6:06
Reboot? That's really terrible advice, DON'T do that, especially when
source /etc/bash.bashrc
does all you want in this example. But should use ~/.bashrc
or ~/.bash_aliases
instead– Xen2050
Jan 18 '18 at 12:50
Reboot? That's really terrible advice, DON'T do that, especially when
source /etc/bash.bashrc
does all you want in this example. But should use ~/.bashrc
or ~/.bash_aliases
instead– Xen2050
Jan 18 '18 at 12:50
@Xen2050 , I suggested reboot to show it works after reboot/relogin. By the way, even another clean terminal window will also work.
– Fahad Ahammed
Jan 19 '18 at 15:52
@Xen2050 , I suggested reboot to show it works after reboot/relogin. By the way, even another clean terminal window will also work.
– Fahad Ahammed
Jan 19 '18 at 15:52
1
1
You can upgrade to a new kernel without rebooting, this ain't old windows ;-)
– Xen2050
Jan 19 '18 at 15:54
You can upgrade to a new kernel without rebooting, this ain't old windows ;-)
– Xen2050
Jan 19 '18 at 15:54
add a comment |
As I recall, bashrc
has, or had, a line suggesting to not use it for aliases directly. The solution is to use an external file(s). The foo
and bar
aliases have been added, but to add baz
the bashrc
file must be "sourced" (or, just open a new terminal). Example as:
thufir@dur:~$
thufir@dur:~$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '''s/^s*[0-9]+s*//;s/[;&|]s*alert$//''')"'
alias bar='echo foo'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias foo='echo foo'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
thufir@dur:~$
thufir@dur:~$ cat .bash_aliases
alias foo='echo foo'
alias bar='echo foo'
alias baz='echo baz'
thufir@dur:~$
thufir@dur:~$ source .bashrc
thufir@dur:~$
thufir@dur:~$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '''s/^s*[0-9]+s*//;s/[;&|]s*alert$//''')"'
alias bar='echo foo'
alias baz='echo baz'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias foo='echo foo'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
thufir@dur:~$
thufir@dur:~$ baz
baz
thufir@dur:~$
now the baz
alias works. I only just now realized that a previous answer had mentioned this technique, but they had buried the lede.
add a comment |
As I recall, bashrc
has, or had, a line suggesting to not use it for aliases directly. The solution is to use an external file(s). The foo
and bar
aliases have been added, but to add baz
the bashrc
file must be "sourced" (or, just open a new terminal). Example as:
thufir@dur:~$
thufir@dur:~$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '''s/^s*[0-9]+s*//;s/[;&|]s*alert$//''')"'
alias bar='echo foo'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias foo='echo foo'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
thufir@dur:~$
thufir@dur:~$ cat .bash_aliases
alias foo='echo foo'
alias bar='echo foo'
alias baz='echo baz'
thufir@dur:~$
thufir@dur:~$ source .bashrc
thufir@dur:~$
thufir@dur:~$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '''s/^s*[0-9]+s*//;s/[;&|]s*alert$//''')"'
alias bar='echo foo'
alias baz='echo baz'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias foo='echo foo'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
thufir@dur:~$
thufir@dur:~$ baz
baz
thufir@dur:~$
now the baz
alias works. I only just now realized that a previous answer had mentioned this technique, but they had buried the lede.
add a comment |
As I recall, bashrc
has, or had, a line suggesting to not use it for aliases directly. The solution is to use an external file(s). The foo
and bar
aliases have been added, but to add baz
the bashrc
file must be "sourced" (or, just open a new terminal). Example as:
thufir@dur:~$
thufir@dur:~$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '''s/^s*[0-9]+s*//;s/[;&|]s*alert$//''')"'
alias bar='echo foo'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias foo='echo foo'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
thufir@dur:~$
thufir@dur:~$ cat .bash_aliases
alias foo='echo foo'
alias bar='echo foo'
alias baz='echo baz'
thufir@dur:~$
thufir@dur:~$ source .bashrc
thufir@dur:~$
thufir@dur:~$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '''s/^s*[0-9]+s*//;s/[;&|]s*alert$//''')"'
alias bar='echo foo'
alias baz='echo baz'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias foo='echo foo'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
thufir@dur:~$
thufir@dur:~$ baz
baz
thufir@dur:~$
now the baz
alias works. I only just now realized that a previous answer had mentioned this technique, but they had buried the lede.
As I recall, bashrc
has, or had, a line suggesting to not use it for aliases directly. The solution is to use an external file(s). The foo
and bar
aliases have been added, but to add baz
the bashrc
file must be "sourced" (or, just open a new terminal). Example as:
thufir@dur:~$
thufir@dur:~$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '''s/^s*[0-9]+s*//;s/[;&|]s*alert$//''')"'
alias bar='echo foo'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias foo='echo foo'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
thufir@dur:~$
thufir@dur:~$ cat .bash_aliases
alias foo='echo foo'
alias bar='echo foo'
alias baz='echo baz'
thufir@dur:~$
thufir@dur:~$ source .bashrc
thufir@dur:~$
thufir@dur:~$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '''s/^s*[0-9]+s*//;s/[;&|]s*alert$//''')"'
alias bar='echo foo'
alias baz='echo baz'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias foo='echo foo'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
thufir@dur:~$
thufir@dur:~$ baz
baz
thufir@dur:~$
now the baz
alias works. I only just now realized that a previous answer had mentioned this technique, but they had buried the lede.
answered Dec 22 '18 at 14:45
ThufirThufir
1,53084394
1,53084394
add a comment |
add a comment |
protected by heemayl Aug 14 '15 at 18:22
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
How did you create an alias??
– karthick87
Dec 15 '10 at 8:05
@karthick87. I have updated my question.
– Bakhtiyor
Dec 15 '10 at 8:17
2
ANSWER -- stackoverflow.com/a/2622711/1487102
– neaumusic
Sep 10 '15 at 18:18