You are not logged in.

#1 2017-04-17 04:23:53

ccerna1
Member
Registered: 2016-03-07
Posts: 26

[SOLVED]Creating aliases and saving them

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

#2 2017-04-17 04:36:48

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED]Creating aliases and saving them

You are missing a source call (the dot):

.  ~/.bash_aliases

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2017-04-17 05:28:03

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,774

Re: [SOLVED]Creating aliases and saving them

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.

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

#4 2017-04-17 05:58:27

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: [SOLVED]Creating aliases and saving them

ccerna1 wrote:

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 StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#5 2017-04-17 06:38:31

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED]Creating aliases and saving them

ewaller wrote:

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) smile


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#6 2017-04-17 14:43:41

ccerna1
Member
Registered: 2016-03-07
Posts: 26

Re: [SOLVED]Creating aliases and saving them

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

#7 2017-04-17 15:04:14

ccerna1
Member
Registered: 2016-03-07
Posts: 26

Re: [SOLVED]Creating aliases and saving them

ewaller wrote:
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

#8 2017-04-17 15:29:56

seth
Member
Registered: 2012-09-03
Posts: 51,046

Re: [SOLVED]Creating aliases and saving them

"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

#9 2017-04-17 17:52:51

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED]Creating aliases and saving them

ccerna1 wrote:

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.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#10 2017-04-17 18:24:40

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: [SOLVED]Creating aliases and saving them

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

Board footer

Powered by FluxBB