You are not logged in.
Pages: 1
Good evening everyone.
Currently diving into bash and alias and came across an odd issue maybe someone can please explain.
I have added this to the .bashrc file so I can keep my aliases on a separate file instead of .bashrc
if [ -f ~/.bash_aliases ]; then
~/.bash_aliases
fi
I create the .bash_aliases
open the file and in enter the alias i want to use.
Save the file and chmod 755 .bash_aliases
I exit the terminal go back in and type the alias and says command not found....
BUT if I go into the .bashrc file and enter the alias there and exit the terminal and go back in type the alias it works...
is putting the alias in the .bashrc file the preferred method?
Thank You!!
Last edited by ccerna1 (2017-04-17 16:05:52)
Offline
You are missing a source call (the dot):
. ~/.bash_aliases
Offline
You are missing a source call (the dot):
. ~/.bash_aliases
Which is why, when writing a script, I prefer the source built in
source ~/bash_aliases
The "source" is much harder to miss than the ".".
OTOH, when typing it from the command line, I use the shorter . notation.
Last edited by ewaller (2017-04-17 05:28:30)
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
Save the file and chmod 755 .bash_aliases
You don't need to make the file executable to source it either (i.e. you can use 644).
My Arch Linux Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone
Offline
The "source" is much harder to miss than the "."
That is probably why it is more portable (the dot, that is: `source` only works in bash & zsh)
Offline
Awesome thank you guys for your patience and help one last item since am new to this.
Can someone please break this code down as to what it should be doing I have a vague idea that it looks for the aliases on a separate dot file:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
Offline
jasonwryan wrote:You are missing a source call (the dot):
. ~/.bash_aliases
Which is why, when writing a script, I prefer the source built in
source ~/bash_aliases
The "source" is much harder to miss than the ".".
OTOH, when typing it from the command line, I use the shorter . notation.
Jason,
so what you are saying is I should use the source instead of the dot?
source ~/.bash_aliases
Offline
"source" and "." are effectively equivalent (merely just syntax variations as "$HOME" and "~")
"source" is more explicit and "." is less to type ;-)
The difference between this and your former approach is that you used to run a script that set aliases, but those only affect the context of that script - not the calling shell.
Instead, you're now sourcing a bunch of lines which is ultimately the same as putting them directly into the basrc file - just more convenient.
Offline
Jason,
so what you are saying is I should use the source instead of the dot?
source ~/.bash_aliases
The dot is portable, source is not. If you are writing for POSIX, then use the dot.
Offline
Note that all of the above is guiding you on sourcing the file. There seems to be an alternative approach you were trying to take: to execute the file as a script. You could do this, but in addition to marking it executable (chmod 0755) which it sounds like you have done, you'd also need to include a shebang in the file (the first line of the alias file would be "#!/bin/bash"). If you do this, there would be no error ... but your aliases wouldn't really work. If you execute the alias file, it runs in a subshell, then exits. All those aliases would only be defined in that very-short-lived subshell. So to get the end result you want, you do need to source (using either "source" or ".") the file, not execute it.
EDIT: upon rereading, maybe you already did have the shebang line and the "command not found" error was from trying to use an alias. But in any case, the above still might explain why sourcing is necessary as opposed to executing the alias file as a script.
"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" - Richard Stallman
Offline
Pages: 1