LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-06-2009, 11:48 AM   #1
cynicalpsycho
Member
 
Registered: Mar 2009
Location: America
Distribution: Debian/Arch
Posts: 134

Rep: Reputation: 16
using alias's and editing the bashrc


Ok, I know I'm being lazy here... but could anyone give me a brief rundown on how to create my own aliases and use them in Linux?

like say if I want to run ls -r but wanted to make the command to do so = lsr

how would I do it? and where do i find my my bashrc and how do i use it to make my aliases persistent?

(i'm running Ubuntu btw)
 
Old 05-06-2009, 11:54 AM   #2
pljvaldez
LQ Guru
 
Registered: Dec 2005
Location: Somewhere on the String
Distribution: Debian Wheezy (x86)
Posts: 6,094

Rep: Reputation: 271Reputation: 271Reputation: 271
Edit the file ~/.bashrc using any text editor (I think Ubuntu uses gedit). It's a hidden file by default. There's examples in the file, but I believe it is simply adding a line
Code:
alias lsr='ls -r'
Where the ' is a single quote or double quote (").

Edit: Corrected to reflect my stupidity. Thanks to i92guboj for pointing out a bad error on my part (using backticks instead of single quotes)...

Last edited by pljvaldez; 05-06-2009 at 02:19 PM.
 
Old 05-06-2009, 11:54 AM   #3
eco
Member
 
Registered: May 2006
Location: BE
Distribution: Debian/Gentoo
Posts: 412

Rep: Reputation: 48
Code:
$ echo 'alias ll="ls -l"' >> ~/.bashrc
$ bash
$ ll
voila!
 
Old 05-06-2009, 02:03 PM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
Quote:
Originally Posted by pljvaldez View Post
Edit the file ~/.bashrc using any text editor (I think Ubuntu uses gedit). It's a hidden file by default. There's examples in the file, but I believe it is simply adding a line
Code:
alias lsr=`ls -r`
Where the ` is the one on the ~ key, not on the " key.
Sorry but the aliases are defined in the following way:

Code:
alias="string"
Usually you don't want to use backticks there, because that will cause the expansion of the contents, which is usually not desired. So, either double or single quotes will do. Like this:

Code:
lsr='ls -r"
or

Code:
lsr="ls -r
What you are doing would result in the expansion of ls -r to the results of running that command. Which would have the following (horrid) result.

Code:
# we define the alias with backticks as you instructed
$ alias lsr=`ls -r`
# we use alias without arguments to see the defined aliases
$ alias
[...]
alias lsr='yobonobo
Xt7BindKeys
win
wallpapers
tonteridas
tmp
src
samba
nwn2
notas importantes
Neverwinter Nights 2
naa..jpg
music
mp3
logs
local
keysymdef.h
kernel
images
hentai
goodstuff
gentoo-overlays
gentoo-doc
games
es.po
downloads
doc
discos
Desktop
clara
chesternobo.jpg
bin
backups
24 abril 2009 mercury.xcf
24 abril 2009 mercury.jpg
24 abril 2009 mercury 3.jpg
24 abril 2009 mercury 2.xcf.1
24 abril 2009 mercury 2.xcf
24 abril 2009 mercury 2.jpg
'
[...]
As you see, the results of "ls -r" have been tied to the alias. Of course, running the alias will result in an error in the best case. But it could be even harmful depending on the output of ls -r... so, your idea is not only wrong, but potentially harmful.

Of course this rule is not carved in stone, there are situations where the backticks might be used, for example, you could do this:

Code:
lsr=`echo ls -r`
Here, `echo ls -r` will be expanded to the result of the echo command, which will be "ls -r", hence giving the alias the correct value. Another question is why would anyone do this... Here it makes no sense of course.



As for where to put it, it depends on the shell and the scope you want. If you use the bash shell then you should put it into both of these files: ~/.bashrc and ~/.bash_profile of your user. Or, if you want to set it globally for all users, then the best best is probably /etc/profile

You can read more about the bash initialization files in the "INVOCATION" section of the bash man page.

Last edited by i92guboj; 05-06-2009 at 02:05 PM.
 
Old 05-06-2009, 02:13 PM   #5
pljvaldez
LQ Guru
 
Registered: Dec 2005
Location: Somewhere on the String
Distribution: Debian Wheezy (x86)
Posts: 6,094

Rep: Reputation: 271Reputation: 271Reputation: 271
Quote:
As you see, the results of "ls -r" have been tied to the alias. Of course, running the alias will result in an error in the best case. But it could be even harmful depending on the output of ls -r... so, your idea is not only wrong, but potentially harmful.
Hmmm. I guess my bad. I'll have to try this when I get home to my linux box. Thanks for the correction!
 
Old 05-06-2009, 04:10 PM   #6
cynicalpsycho
Member
 
Registered: Mar 2009
Location: America
Distribution: Debian/Arch
Posts: 134

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by i92guboj View Post
As for where to put it, it depends on the shell and the scope you want. If you use the bash shell then you should put it into both of these files: ~/.bashrc and ~/.bash_profile of your user. Or, if you want to set it globally for all users, then the best best is probably /etc/profile
Why does it need to be in ~/.bash_profile as well?
and would putting it in /etc/profile be the same as in bashrc? merely putting it at the end of the file?
 
Old 05-06-2009, 04:13 PM   #7
cynicalpsycho
Member
 
Registered: Mar 2009
Location: America
Distribution: Debian/Arch
Posts: 134

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by eco View Post
Code:
$ echo 'alias ll="ls -l"' >> ~/.bashrc
$ bash
$ ll
voila!
That's friggan beautiful man... *tear*
 
Old 05-06-2009, 04:26 PM   #8
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
Quote:
Originally Posted by cynicalpsycho View Post
Why does it need to be in ~/.bash_profile as well?
and would putting it in /etc/profile be the same as in bashrc? merely putting it at the end of the file?
~/.bashrc is read when initializing an interactive non-login session (i.e. xterm, konsole or whatever). ~/.bash_profile is read when starting an interactive login session (for example, if you enter via ssh from another machine, or if you login into the text console to do some administrative task). So, both are unrrelated and you probably want to have your alias in both cases, don't you?

As I said in my other post, all this info (and more about initialization files) can be found on the bash man page, section "INVOCATION".

As I also explained above, /etc/profile can be considered a global configuration file for bash. If you use ~/.bashrc and/or ~/.bash_profile the alias will be for your user only. If you use /etc/profile the alias will be for all the users (provided that they all user bash as their shell of choice).

The disadvantage is that if you use /etc/profile, that file might be overwritten the next time you install/update bash, so beware of that. On the contrary, the package manager will never overwrite the files you save in your home (like ~/.bashrc or ~/.bash_profile).
 
Old 05-06-2009, 08:34 PM   #9
cynicalpsycho
Member
 
Registered: Mar 2009
Location: America
Distribution: Debian/Arch
Posts: 134

Original Poster
Rep: Reputation: 16
I'm having a bit of a problem finding bash_profile, I find a file called "profile" is that the file i'm looking for?
 
Old 05-06-2009, 08:54 PM   #10
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
Quote:
Originally Posted by cynicalpsycho View Post
I'm having a bit of a problem finding bash_profile, I find a file called "profile" is that the file i'm looking for?
These files don't exist by default, they have to be created inside the home directory for the involved user. Note that all of these start with a dot, so they are usually hidden even if they are there.

I am going to paste the said section of the bash man page for your reference here:

Quote:
Originally Posted by bash man page
INVOCATION
A login shell is one whose first character of argument zero is a -, or one started with the --login option.

An interactive shell is one started without non-option arguments and without the -c option whose standard input and error are both connected
to terminals (as determined by isatty(3)), or one started with the -i option. PS1 is set and $- includes i if bash is interactive, allowing
a shell script or a startup file to test this state.

The following paragraphs describe how bash executes its startup files. If any of the files exist but cannot be read, bash reports an error.
Tildes are expanded in file names as described below under Tilde Expansion in the EXPANSION section.

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes com‐
mands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.pro‐
file, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used
when the shell is started to inhibit this behavior.

When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.

When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This
may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of
~/.bashrc.
Please, read it, as I already told you two (three now) times. Specially look at this paragraph:

Quote:
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes com‐
mands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.pro‐
file, in that order, and reads and executes commands from the first one that exists and is readable.
That's why I told you to use ~/.bash_profile, because if either ~/.bash_profile or ~/.bash_login exist (even if it's empty) then whatever you put into ~/.profile will never ever be executed. Only one of these files is run when bash is initialized, the first one found, in this same order: ~/.bash_profile, ~/.bash_login, and ~/.profile, and the rest, if any, are completely ignored.

Understand also that these three files are only for login shells, as I already told you in the other post. For the rest (xterms and the like) you need ~/.bashrc.

If you have any doubts ask as much as you need, but please, first, read the "INVOCATION" section in the man page.
 
Old 05-07-2009, 01:31 AM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
Actually, (at least on my system) .bash_profile sources .bashrc anyway, by default ...
I'm pretty sure I've seen that on Fedora & RH as well.
Don't know if other distros do that or not ...
 
Old 05-07-2009, 01:41 AM   #12
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
Quote:
Originally Posted by chrism01 View Post
Actually, (at least on my system) .bash_profile sources .bashrc anyway, by default ...
It will source whatever file you want as long as you have a line to do so inside the script. That's no mystery. You could as well symlink both if you wish. But that's besides the point that they are two different files, with two different purposes. They both exist for a reason.

Maybe these distros do prebuilt init files for bash in /etc/skel. Whatever is into that directory will be copied automatically to every new user account. That can be used to put default .bashrc and .bash_profile files there.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
~/.bashrc ; Editing. User Name. Linux - Newbie 8 01-01-2007 01:52 AM
Editing .bashrc AceKidCool Linux - Newbie 3 11-27-2003 11:46 PM
bash env vars alias's & .bashrc micxz Linux - General 8 10-08-2003 03:09 PM
~/.bashrc editing TX_metalhead Linux - General 1 07-19-2002 03:29 PM
Editing /etc/bashrc qadir Linux - Software 2 07-06-2002 05:14 AM


All times are GMT -5. The time now is 05:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration