My alias name is referring to old alias not a new one [duplicate] - linux

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What's the difference between .bashrc, .bash_profile, and .environment?
It seems that if I use
alias ls='ls -F'
inside of .bashrc on Mac OS X, then the newly created shell will not have that alias. I need to type bash again and that alias will be in effect.
And if I log into Linux on the hosting company, the .bashrc file has a comment line that says:
For non-login shell
and the .bash_profile file has a comment that says
for login shell
So where should aliases be written in? How come we separate the login shell and non-login shell?
Some webpage say use .bash_aliases, but it doesn't work on Mac OS X, it seems.

The reason you separate the login and non-login shell is because the .bashrc file is reloaded every time you start a new copy of Bash. The .profile file is loaded only when you either log in or use the appropriate flag to tell Bash to act as a login shell.
Personally,
I put my PATH setup into a .profile file (because I sometimes use other shells);
I put my Bash aliases and functions into my .bashrc file;
I put this
#!/bin/bash
#
# CRM .bash_profile Time-stamp: "2008-12-07 19:42"
#
# echo "Loading ${HOME}/.bash_profile"
source ~/.profile # get my PATH setup
source ~/.bashrc # get my Bash aliases
in my .bash_profile file.
Oh, and the reason you need to type bash again to get the new alias is that Bash loads your .bashrc file when it starts but it doesn't reload it unless you tell it to. You can reload the .bashrc file (and not need a second shell) by typing
source ~/.bashrc
which loads the .bashrc file as if you had typed the commands directly to Bash.

Check out http://mywiki.wooledge.org/DotFiles for an excellent resource on the topic aside from man bash.
Summary:
You only log in once, and that's when ~/.bash_profile or ~/.profile is read and executed. Since everything you run from your login shell inherits the login shell's environment, you should put all your environment variables in there. Like LESS, PATH, MANPATH, LC_*, ... For an example, see: My .profile
Once you log in, you can run several more shells. Imagine logging in, running X, and in X starting a few terminals with bash shells. That means your login shell started X, which inherited your login shell's environment variables, which started your terminals, which started your non-login bash shells. Your environment variables were passed along in the whole chain, so your non-login shells don't need to load them anymore. Non-login shells only execute ~/.bashrc, not /.profile or ~/.bash_profile, for this exact reason, so in there define everything that only applies to bash. That's functions, aliases, bash-only variables like HISTSIZE (this is not an environment variable, don't export it!), shell options with set and shopt, etc. For an example, see: My .bashrc
Now, as part of UNIX peculiarity, a login-shell does NOT execute ~/.bashrc but only ~/.profile or ~/.bash_profile, so you should source that one manually from the latter. You'll see me do that in my ~/.profile too: source ~/.bashrc.

From the bash manpage:
When bash is invoked as an
interactive login shell, or as a
non-interactive shell with the
--login option, it first reads and executes commands from the file
/etc/profile, if that file exists.
After reading that file, it looks for
~/.bash_profile, ~/.bash_login, and
~/.profile, 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.
Thus, if you want to get the same behavior for both login shells and interactive non-login shells, you should put all of your commands in either .bashrc or .bash_profile, and then have the other file source the first one.

.bash_profile is loaded for a "login shell". I am not sure what that would be on OS X, but on Linux that is either X11 or a virtual terminal.
.bashrc is loaded every time you run Bash. That is where you should put stuff you want loaded whenever you open a new Terminal.app window.
I personally put everything in .bashrc so that I don't have to restart the application for changes to take effect.

Related

Why must I run . ~/.bashrc each time I want to access my shortcuts there?

To speed up some of my Linux commands, I followed the recommendations of this Lifehacker article by writing aliases to quickly cd into folders I like. Fran's answer to "source" the .bashrc file by executing this line enabled me to use my shortcuts:
. ~/.bashrc
Like the OP in this post, it is unclear to me how to always access my shortcuts without having to rerun that command.
This is an issue lots of people run into when new to Bash. It comes from not understanding which files are read when.
Your .profile or .bash_profile is read when you log on. This is a login shell.
Your .bashrc is run whenever you start another interactive shell.
Typically you will want to include a source ~/.bashrc as the last thing that happens in your profile to ensure that your login shell has these same aliases.
Per the bash man page:
When an interactive shell that is not a login shell is started, bash
reads and executes commands from ~/.bashrc, if that file exists.
Therefore, every time you open a new interactive bash shell (such as when you open a new terminal), ~/.bashrc will automatically be read.

modifying /etc/profile linux

I need to change the greeting of user, which is logging in. So I modifyed file /etc/profile. In this greeting I need to know, which shell this user use and tell it to user. The problem is that then I change my shell on zsh or csh it doesnt work. Even if I just type in this file echo $SHELL it do nothing. As I think, when I use csh and zsh this file (/etc/profile) doesnt run at all. How can I fix this problem?
Thanks you, sorry for my English)
You should start by reading the manpage of every shell on your system.
There are different flavours of shells. Each flavours uses slightly different (per session and per shell, per site and per user) initialisation files. For example:
sh (and bash) use /etc/profile and ~/.profile
bash also uses ~/.bash_profile, ~/.bashrc, ~/.bash_logout
csh uses /etc/.login and ~/.cshrc
etc...
The above list is not meant to be exhaustive. It is to illustrate you will need to check the exact behaviour of each shell that is used on your system and configure it appropriately.
You also need to consider whether you want to change system-wide behaviour (corresponding to initialisation files under /etc) or user-specific behaviour (corresponding to initialisation files in the user's home directory).
For certain shells, there's also per-session (i.e. once per login) and per-shell settings (e.g. for every terminal window). A good example is ~/.bash_login (executed once per login) and ~/.bashrc (executed for every shell - e.g. terminal window).
They both execute different files:
From fro zsh http://zsh.sourceforge.net/Guide/zshguide02.html
Now here's a list of the startup files and when they're run. You'll
see they fall into two classes: those in the /etc directory, which are
put there by the system administrator and are run for all users, and
those in your home directory, which zsh, like many shells, allows you
to abbreviate to a `~'.
/etc/zshenv
Always run for every zsh.
~/.zshenv
Usually run for every zsh (see below).
/etc/zprofile
Run for login shells.
~/.zprofile
Run for login shells.
/etc/zshrc
Run for interactive shells.
~/.zshrc
Run for interactive shells.
/etc/zlogin
Run for login shells.
~/.zlogin
for csh http://unixhelp.ed.ac.uk/CGI/man-cgi?csh+1
A login shell begins by executing commands from the system files
/etc/csh.cshrc and /etc/csh.login.
You can make a soft link to point to the same file:
ln -s /etc/profile /etc/zshenv
ln -s /etc/profile /etc/csh.login
I have modified my etc/profile file to start a python script on startup. now my program is running but there is a black screen, because my program has a while True loop in it and now I am not able to stop it. Kindly tell me how to stop the program, I have tried ctrl+C but nothing happened.

Why do exported variables but not aliases from .bash_profile work in non-login shells?

I'm using the Fedora 20 graphical desktop. I found the alias put in the .bash_profile didn't have effect. Then I find the graphical terminal is not a login shell, so the bash_profile is not read at all.
Now it's weird to me that the export command does have effect in .bash_profile.
My .bash_profile is as below:
#bash_profile
export mytest=bash_profileIsRead
alias kk=ls
Test result:
$ shopt login_shell
login_shell off
$ echo $mytest
bash_profileIsRead
$ kk
bash: kk: command not found...
There's nothing unusual or surprising about this.
Your .bash_profile is run once per session, by your login shell. It is not run by other shells run later in your session.
.bashrc, by contrast, is run by every interactive shell instance, so things like aliases and shell functions placed there will be honored throughout the session.
Environment variables only need to be set once, because they're inherited by subprocesses (every subprocess, not just shells!). Aliases are not inherited, so they need to be set in every shell.
See the DotFiles page on the wooledge.org wiki (maintained by irc.freenode.org's #bash channel) for more.
Aliases are not inherited like environment variables. They should not be placed in profile, but instead in the .bashrc file.
Basically, .profile (or .bash_profile) is for things that are inherited (e.g. env variables) and the rc file is for things that must be re-initialized in non-login shells, such as aliases.

How to set PATH in Knoppix?

I'm using knoppix 7.0.3 and trying to set the PATH environment variable. According to the official Ubuntu documentation, /etc/environment should be the preferred place for this. So I added these lines in the file:
JAVA_HOME="/usr/lib/jvm/java-6-sun"
GRAILS_HOME="/home/knoppix/grails"
PATH="${PATH}:${JAVA_HOME}/bin:${GRAILS_HOME}/bin"
But after rebooting the system, the file just reverted to the original one (I was using persistent storage).
Then after some Googling, I tried to edit ~/.profile like this:
export JAVA_HOME="/usr/lib/jvm/java-6-sun"
export GRAILS_HOME="/home/knoppix/grails"
export PATH=$PATH:$JAVA_HOME/bin:$GRAILS_HOME/bin
This time, the first two variables got set (echoed in console), but the PATH didn't. It was still the default one when I echoed. What's wrong?
Modify /etc/profile on the following line:
PATH=".:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
The problem is that your PATH is being overwritten during initialization of bash, after reading .profile.
From the manpage of bash:
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, 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 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.
From your experience, it is apparent that if .bashrc doesn't exist, bash is trying to set PATH to a default value (I would appreciate if someone confirms this).
As we discussed in the comments on your question, adding the export commands to .bashrc (and thus creating the file) solves the problem. Alternatively, you can add
source ~/.profile
to the end of your .bashrc file for the same effect.

Profile is not loaded for all the users

I recently got arm-linux-gcc toolchain up and running for my Ubuntu OS. I have configure the path variable for the toolchain in /etc/profile file. I am able to access the toolchain only when I am logged in as root and that too, I have to do
source /etc/profile
in every session. Why does this happen? why am I not able to access the toolchain from normal login.
Regards,
Probably you're not running a login-shell.
It depends a bit on the shell you're using. I use Bash, but maybe you use BusyBox?
From the bash manual:
When bash is invoked as an interactive
login shell, or as a non-interactive
shell with the --login option, it
first reads and executes commands
from the file /etc/profile, if that
file exists. After reading that file,
it looks for ~/.bash_profile,
~/.bash_login, and ~/.profile, in that
order, and reads and executes commands from the first one that
exists and is readable.
What I usually do to resolve this issue, is put:
source /etc/profile
as first line in my ~/.bashrc.
This works because ~/.bashrc is also executed for non-login shells.
To see which files are executed, you might want to put diagnostic messages in them. If I can't remember which files are executed at what type I login, I put:
echo /etc/profile/
as first line in my /etc/profile, the same for /etc/bash/bashrc, ~/.bashrc, ~/.profile and so on.
For more info, consult the "INVOCATION" part of the bash-manual.

Resources