why .bashrc not executed when open a new terminal? - linux

According to the bash startup description, .bashrc should be reloaded when doing a non-login interactive shell.
When an interactive shell that is not a login shell is started, Bash reads and executes commands from ~/.bashrc, if that file exists.
I append a new variable into .bashrc like NAME="TEST_BASHRC" then save it.
But when open a new terminal, the variable $NAME is still None. That is to say, .bashrc isn't reloaded when opening new terminal.
Why .bashrc not executed and how can I make .bashrc reload when opening new terminal?
The following is what I do:
# comment: append variable "NAME" to .bashrc
$ cat .bashrc
export NAME="TEST_BASHRC"
# comment: open a new terminal then...
$ echo $NAME
$

You need to export the variable:
Try:
export NAME="TEST_BASHRC"
See why here

Related

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

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.

Environment variable PATH on linux

Hi i am currently trying to set keywords for my terminal to launch some software without having to type the whole path.
For exemple:
firefox
#instead of
/home/debian/firefox/firefox
I always do this kind of thing on windows by setting path in the environment variable manager.
After i read this post PATH environment variable in linux , i added this line to the etc/environment file:
export firefox=/home/debian/firefox/firefox
#I also tried this:
export PATH=$PATH:/home/debian/firefox
It doesn't work, can someone explains me how to do this?
I would setup a new alias in my .bashrc or .profile, which should be located under your home directory. Add the following to the end of the file:
alias firefox="/home/debian/firefox/firefox"
Save the file and reload it using:
source ~/.bashrc
Since you added the alias to your .bashrc this alias will be created everytime you open a new instance of a shell.
You could use nohup to keep the command running after the shell session ends:
alias firefox="nohup /home/debian/firefox/firefox &"
Notice the trailing & character, which will run the command in the background so you can keep using your terminal.
You can also make an alias in your .bashrc file.
$ vim ~/.bashrc
It will open your .bashrc in read mode. Get in write mode by pressing i. You can create alias at anywhere in the file or below already created alias list.
alias firefox='/home/debian/firefox/firefox'
press Esc and then :wq
This will create your alias, save and exit the file. Now you only have to compile .bashrc by this
$ source ~/.bashrc
After this you can only have to use firefox instead of long /home/debian/firefox/firefox
Adding /home/debian/firefox to your PATH should have done it.
Did you start a new shell after making that change? Otherwise the new PATH would not have exported yet. Alternatively, you can just run export PATH=$PATH:/home/debian/firefox directly to update it for your current session.

run a command on the remote machine after ssh login

I would like to switch to the bash environment after ssh login. Currently I have to type bash every time and then cd to my working directory.
Where can I add some settings so it will run automatically for me.
There's an environment variable SHELL that is set to the current shell. You can set it to your preferred shell by running
$ export SHELL=/path/to/shell
In order to find the path to your preferred shell just run following command
$ whereis bash
Enter the path returned by whereis command as your shell path.
There's a .profile(ls -al) or bash_profile that you can add your setting there. If you can't find such a file then create one using touch .profile. (I did create this file on OS X.)
Open the .profile in order to edit it using whatever text editors that you want
$ vim .profile
Then change the $HOME environment variable in order to change your default home directory path. Enter below line in opened file
export HOME=/your/path
Save the file
:w

Can you run a script every time /bin/sh is invoked?

With bash, you can set your ~/.bashrc file to run something every time a new bash shell is created. Is it possible to do the same thing with /bin/sh? (This is on Debian, by the way).
For now, I just want to echo 'I am sh' when /bin/sh is invoked. It's easy to do in bash ("echo 'I am bash'" placed at the top of the file).
Thanks!
When starting a login shell of dash, which is /bin/sh on debian-like systems, it will read ~/.profile. If you also want a configuration file read for interactive non-login shells, add the following line to your ~/.profile file:
ENV=$HOME/.shinit; export ENV
Then, with the variable ENV appearing in the environment, the file $HOME/.shinit will be sourced with every new interactive (dash) shell.
You may change the file name specified by ENV to any file name you prefer.
To assure a dash login shell has added ENV to the environment, you may need to logout and log back in, or possibly reboot, depending on your system setup.
Documentation
This is documented in man dash:
A login shell first reads
commands from the files /etc/profile and .profile if they exist. If the environment variable ENV
is set on entry to an interactive shell, or is set in the .profile of a login shell, the shell
next reads commands from the file named in ENV. Therefore, a user should place commands that are
to be executed only at login time in the .profile file, and commands that are executed for every
interactive shell inside the ENV file.
Example
Suppose that we have files set up like:
$ echo "ENV=$HOME/.shinit; export ENV" >>~/.profile
$ cat .shinit
echo FOUND ME
Since I just added the ENV line to the ~/.profile file, ENV is not yet in the environment. If we run dash:
$ dash
$
Nothing happened because this is a non-login shell and ENV is not yet in the environment.
If we start a login shell, ENV is placed in the environment and ~/.shinit is run:
$ dash -l
FOUND ME
If, as a child of that shell, we run an interactive non-login shell, then ~/.shinit will be run because the parent shell created the ENV variable:
$ dash
FOUND ME
The environment created by the login shell above only affects its children. To assure that all interactive dash shells have ENV in their environment may, as mentioned above, require logging out and back in, or a reboot.

How do I set an environment variable in cygwin?

In linux I would go:
setenv -p MYVAR "somevalue"
But this doesn't seem to work in cygwin.
By default Cygwin is running the Bourne shell or Bash, so the command to set a variable is different. This is the code you need:
export MYVAR="somevalue"
The export part lets the shell know that it is an environment variable instead of a local variable.
If you type ls -a in your home directory, you should see some or all of the following files:
.bashrc
.bash_profile
.profile
.bash_profile is executed for login shells, and .bashrc is executed for interactive non-login shells. To most simply ensure that your environment variable is always set, open up .bash_profile and add the text:
export MYVAR="somevalue"
Your shell with then execute .bash_profile every time it starts up, and it will run this command. You will then have the MYVAR variable accessible all of the time. If you didn't export the variable, it would only be accessible within your .bash_profile file.
You can check that this variable is defined by printing its value to your shell:
echo $MYVAR
You can delete (unset) the variable with:
unset $MYVAR
Brief words on shell config files
As an aside, regarding .bashrc vs .bash_profile vs. .profile, see these answers:
difference between .bash_profile and .bashrc
difference between .profile and .bash_profile
For simplicity of configuration, I recommend sourcing your .bashrc file from .bash_profile. Add this to .bash_profile:
if [ -f ${HOME}/.bashrc ]; then
source ${HOME}/.bashrc
fi
This will load .bashrc from .bash_profile.
If you do this, you can instead put the following line in .bashrc, if you wish:
export MYVAR="somevalue"
The best way to set up environment variables in cygwin is to create a bash profile and execute that profile everytime you login and run the shell.
In my .bash_profile file , this is the setting I have
JAVA_HOME = C:/Program Files/Java/jdk1.7.0_51
export JAVA_HOME
export PATH=$PATH:$JAVA_HOME/bin
Once you run bash, check out echo $JAVA_HOME and you should see the path as output.

Resources