Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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)...
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.
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!
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?
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).
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.
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 ...
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.