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

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.

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.

Sourcing from a different bashrc

My original .bashrc script is currently used to run model runs. Now I need to manipulate it to compile a completely new model.
My question is, if I save my original .bashrc, as something such as .bwwbashrc, do I need to manipulate the file in some way so it is able to be read or recognized as the .bashrc when I source it within my scripts?
original sourcing
source /home/tsee/.bashrc
What I think the new sourcing would be.
(after creating .bwwbashrc)
source /home/tsee/.bwwbashrc
Just not sure if I need to save it with a certain extension or edit the executable in some sort of way.
Nope, you can name it whatever you want. Executable bit isn't required either.
If you aren't aware of it, the bash --login option might be of interest to you.
To complement Matt’s correct answer, I’d also point out that you can start a new Bash shell that sources your alternative file instead of .bashrc at start-up.
bash --rcfile .bwwbashrc
From the bash man page:
--rcfile file
Execute commands from file instead of the standard personal initialization
file ~/.bashrc if the shell is interactive (see INVOCATION below).
If you want to replace your current shell (with commands and settings from .bashrc), you can run
exec bash --rcfile .bwwbashrc

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.

what is .bashrc - How to find the startup file - putty

I was going over this article and it states in step 3
Add the following to your .bashrc (or the appropriate startup file for your shell) To use it immediately, be sure to type “source .bashrc”
Any idea on how I could know what my startup file is ? I am using putty ?
Once you use putty to SSH into your server, you can run "ls -al .bashrc" and it should show you the file, edit this with an editor you know, if none, then use vi like this "vi .bashrc".
Go to where you need to edit the file and type in "i" to put vi in Insert mode. Next type in your text. Once you are done press the escape button and ":wq", no quotes for the i or :wq.
Next you can source it by typing "source .bashrc" and the setting you added should be part of your BASH shell environment now.
The .bashrc is a file which is called by bash before on each start of a new interactive shell. The file can be used to setup the environment, export variables, create aliases and functions and more...
There are usually multiple instances of that file. One per system and one per user to allow system wide configuration but also customization by users ( users bashrc will be sourced after the system wide bashrc and can overwrite things). I suggest to add the lines to your user's bashrc first. The file is located in your home folder. Type:
vi $HOME/.bashrc
in order to edit the file. If you aren't familiar with the vi editor you can choose an editor of your choice like nano, mcedit or even a GUI text editor, but mind that a GUI editor's file dialog may hide the file because it's name starts with a .
Once you managed to edit the file, start a new connection or simply type
source $HOME/.bashrc
in order to parse the file
A path which will work with any bash shell regardless of operating system (macOS/Linux/BSD etc.) is:
~/.bashrc
check your home directory ...because it exists in user's home directory.
check /home/username/ on your terminal if you are using RHEL or CentOS.
.bashrc and .bash_profile are bash config files (bash shell script) that bash runs(execute) whenever it is started interactively. It initializes an interactive (non-login) shell session and the config is read from these files $HOME/.bashrc
.bashrc is a standard hidden file located in your home directory.It determines the behaviour of interactive shells.
.bashrc runs on every interactive shell launch.If you say: $bash
For login shells, the config is read from these files:
/etc/profile (always sourced)
$HOME/.bash_profile (the rest of these files are checked in order until one is found,then no other are read)
$HOME/.bash_login
$HOME/.profile
For example: I added an echo to my .bashrc and .bash_profile files and whenever I called bash or bash -l command in terminal it showed me the echo.

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