Appending bash alias to .bashrc doesn't work - bash

I want to create a alias of my cd command. I have created the .bashrc file and append the command cd ...... to it. (Since the file was newly created, it just has this one line that I added).
After that, only after I typed . ~/.bashrc, can the alias works. If I close the terminal and open it again, I need to retype . ~/.bashrc.
It's really annoying to do this every time. Is there any way to solve this problem?
Thank you so much for help

When you login to linux system, only ~/.profile will be called:
$ cat ~/.profile
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
You need to source ~/.bashrc inside ~/.profile manaully.
Read this to learn more.
EDIT:
If you're using iTerm2 on mac, it actually start a login shell by default when open tabs.
But you can change it: Preferences > General > Command

If using OS X, append the alias to ~/.bash_profile.
You could also add alias to ~/.bashrc, then add source ~/.bashrc to ~/.bash_profile.
Better yet, put all your aliases in ~/.aliases, and source it in ~/.bash_profile.
By default, OS X first sources /etc/bashrc (which shouldn't be modified unless absolutely necessary), then sources the user's ~/.bash_profile at the start of every interactive session.

Related

Why must I source .bashrc every time I open terminal for aliases to work?

Git was working fine. I have created an alias in Git but the issue is when I tried to reopen the terminal, then I need to run . ~/.bashrc every time in the terminal.
What is the best way I don't need to provide source every time when I reopen the terminal?
What I did?
I am trying to add source of the .bashrc file in this file but it is a read-only file. I am not able to add the source of the .bashrc file in this profile.
open /etc/profile
Added the permission to write in the profile as well, still not able to link the source file.
sudo chmod u+w /etc/profile
Profile:
# System-wide .profile for sh(1)
if [ -x /usr/libexec/path_helper ]; then
eval `/usr/libexec/path_helper -s`
fi
if [ "${BASH-no}" != "no" ]; then
[ -r /etc/bashrc ] && . /etc/bashrc
fi
It looks like your terminal emulator is launching bash as a login shell.
If that's the case, it will read /etc/profile for configuration as well as 1 of the following files, if they exist (listed in order of importance) :
~/.bash_profile
~/.bash_login
~/.profile
It will thus ignore your .bashrc file. A correct fix for your situation would be to either configure your terminal emulator to run bash interactively and non-login, or add the following line to your ~/.bash_profile :
[ -f "$HOME/.bashrc" ] && . "$HOME/.bashrc"
Here is a link to the documentation about which files are loaded depending of the type of shell you are running
As per #Aserre's answer i have followed this step to solve this issue
A typical install of OS won't create a .bash_profile for you. When you want to run functions from your command line, this is a must-have.
Start up Terminal
Type cd ~/ to go to your home folder
Type touch .bash_profile to create your new file.
Edit .bash_profile with your favorite editor (or you can just type open -e .bash_profile to open it in TextEdit.
[ -f "$HOME/.bashrc" ] && source "$HOME/.bashrc" Save it and close it
Restart the terminal, It should work
You should write this line source .profile inside your .zshrc file. This is because default shell is zsh. If u don't want to do this solution than u can go for changing the default shell by typing the following command chsh -s /bin/bash then restart your machine or virtual machine. Then no need for source. I hope this will help :) TAKE CARE
If you are using Linux and you want variables set, to persist.
Follow the below steps.
Be the root user -> sudo su
go to etc folder -> cd /etc
open the file bashrc with the editor of your choice -> vi bashrc
set the variable with export command like here I am setting JAVA_HOME ->
export JAVA_HOME=pathHere
Load the bashrc file with command ->
. bashrc
remember to put the dot/period before bashrc.
now JAVA_HOME should be set permanently.
Thanks...

Issue with bashrc file

When I open the .bashrc file using gedit, or vi editor, there's nothing in it. I am on MINT trying to install Hadoop and one of the step requires editing the .bashrc file, to save the environment variable. I remember doing the same on Ubuntu, and there were contents were in it.
Can you help me get them all back? Is it possible to get them all back?
When I do gedit ~/.bashrc it opens to a blank page.
Thanks in advance!
From looking at the MINT forums, it doesn't look like MINT supplies a default .bashrc:
http://forums.linuxmint.com/viewtopic.php?f=90&t=130358
However, .bashrc is an optional file, so you can add whatever you want to it. Check out the bash man pages:
man bash
Also, if you setup a .bashrc file, make sure it is getting called from either .profile or .bash_profile like this:
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
You should have one in here:: /etc/skel/.bashrc
Try copying it to your home folder:: cp /etc/skel/.bashrc ~/.bashrc
Look for .bashrc.swap files in your home directory by doing so:
ls -Fa ~ | grep '*bashrc*'
I don't know how gedit or other programs name their swap files, but just look what comes up.
If there are no such files, you're out of luck.

How to load ~/.bash_profile when entering bash from within zsh?

I've used bash for two years, and just tried to switch to zsh shell on my OS X via homebrew. And I set my default (login) shell to zsh, and I confirmed it's set properly by seeing that when I launch my Terminal, it's zsh shell that is used in default.
However, when I try to enter bash shell from within zsh, it looks like not loading ~/.bash_profile, since I cannot run my command using aliases, which is defined in my ~/.bash_profile like alias julia="~/juila/julia", etc.. Also, the prompt is not what I set in the file and instead return bash-3.2$.
For some reasons, when I set my login shell to bash, and enter zsh from within bash, then ~/.zshrc is loaded properly.
So why is it not loaded whenever I run bash from within zsh? My ~/.bash_profile is symbolic linked to ~/Dropbox/.bash_profile in order to sync it with my other computers. Maybe does it cause the issue?
Open ~/.zshrc, and at the very bottom of the file, add the following:
if [ -f ~/.bash_profile ]; then
. ~/.bash_profile;
fi
Every time you open the terminal, it will load whatever is defined in ~/.bash_profile (if the file exist). With that, you can keep your custom settings for zsh (colors, and etc). And you get to keep your custom shell settings in .bash_profile file.
This is much cleaner than using bash -l IMO.
If you prefer putting your settings in .bashrc, or .bash_login, or .profile , you can do the same for them.
Similarly, you could also move the common profile settings to separate file, i.e. .my_common_profile, and add the following to both .bash_profile and .zshrc:
if [ -f ~/.my_common_profile ]; then
. ~/.my_common_profile;
fi
An interactive bash reads your ~/.bash_profile if it's a login shell, or your ~/.bashrc if it's not a login shell.
A typical .bash_profile will contain something like:
if [ -f ~/.bashrc ]; then . ~/.bashrc; fi
so .bashrc can contain commands to be executed by either login or non-login shells.
If you run bash -l rather than just bash, it should read your .bash_profile.
Reference: https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html
For those who have just installed zsh and want their alias from bash to work on zsh do the following
Open .zshrc file in vim like so
vi ~/.zshrc
Scroll to the bottom
click "i" to enable write mode
Tell zsh to load items from bash_profile when needed like so
source ~/.bash_profile
Write and quit like so
:wq
Refresh your zsh like so
source ~/.zshrc
That's it. Now all your saved alias in .bash_profile will be ready to use in zsh.
To complement #Keith Thompson's excellent answer:
macOS:
As #chepner puts it succinctly (emphasis mine):
In OS X, bash is not used as part of the initial [at boot time] login process, and the Terminal.app (or other terminal emulators) process exists outside any pre-existing bash sessions, so each new window [or tab - read: interactive bash shell] (by default) treats itself as a new login session.
As a result, some OSX users only ever create ~/.bash_profile, and never bother with ~/.bashrc, because ALL interactive bash shells are login shells.
Linux:
On Linux, the situation is typically reversed:
bash shells created interactively are [interactive] NON-login shells, so it is ~/.bashrc that matters.
As a result, many Linux users only ever deal with ~/.bashrc.
To maintain bash profiles that work on BOTH platforms, use the technique #Keith Thompson mentions:
Put your definitions (aliases, functions, ...) in ~/.bashrc
Add the following line to ~/.bash_profile
[[ -f ~/.bashrc ]] && . ~/.bashrc
Copy the contents from ~/.bash_profile and paste them at the bottom of ~/.zshrc file.
For ZSH users on MacOs, I ended up with a one liner.
At the very bottom of the ~/.zshrc I added the following line :
bash -l
What it does is simply load the .bash_profile settings(aliases, function, export $PATH, ...)
If you decide to get rid of ZSH and go back to plain BASH, you'll be back to normal with no hassle at all.
If this is something that you do infrequently, or it just isn't appropriate to make changes, you can also 'source' the .bash_profile after launching the child bash shell.
. ~/.bash_profile
This will pull in the settings you make in the .bash_profile script for the life of that shell session. In most cases, you should be able to repeat that command, so it's also an easy way to test any changes that you make without needing to do a full login, as well as bring all of your existing shell sessions up-to-date if you make upgrades to the .bash_profile &/or .bashrc files.
For macOS Big Sur (Version 11.5.2)
Open .zshrc
For example: sudo nano ~/.zshrc
At the end of the file add source ~/.bash_profile
Every time you open the terminal the contents inside the bash profile will be loaded.
Recently I installed oh-my-zsh on OS X and set zsh as default shell and faced the same problem.
I solved this problem by adding source ~/.bash_profile at the end of .zshrc file.
I am using a zsh framework called oh my zsh and I have tried most of the solutions listed here and it broke the format for my custom theme. However, these steps worked for me.
Add new alias(es) at the bottom of my .bash_profile
vi ~/.bash_profile
Make zsh to load items from .bash_profile
source ~/.bash_profile
Refresh zsh
source ~/.zshrc
Restart OSX Terminal app
Try your new alias!
If you'd like to be "profile-centric", you can create .profile as a single source of truth, then load it from both .bash_profile and .zprofile.
.profile
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
# etc., etc.
.bash_profile and .zprofile
if [ -f ~/.profile ]; then
. ~/.profile;
fi
I found this helped bash scripts find the right PATH, etc., and helped me keep configuration in one place.

Bash .profile not loading

I'm not sure what's happened but my ~/.profile is no longer loading.
Can anyone see something wrong with the following?
export PS1="\u#local [\w]# "
export EDITOR="subl -w"
export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced
alias vst="ssh -i ~/.ssh/vst root#vst"
I know for a fact using that PS1 like I am attempting to do it should be doing Peter#local [~/path/to/file]# but it's not.
Any ideas?
Does ~/.bash_profile or ~/.bash_login exist? If so, that'll be used instead of ~/.profile.
In Unix FAQ (for OS X) we can read:
Bash Startup Files
When a "login shell" starts up, it reads the file
/etc/profile and then ~/.bash_profile or ~/.bash_login or
~/.profile (whichever one exists - it only reads ONE of these,
checking for them in the order mentioned).
When a "non-login shell" starts up, it reads the file /etc/bashrc and then the file ~/.bashrc.
Note that when bash is invoked with the name sh, it tries to mimic the startup sequence of the Bourne shell (sh). In particular, a non-login shell invoked as sh does not read any dot files by default. See the bash man page for details.
So if you already have ~/.bash_profile, the file ~/.profile won't be automatically read by bash, therefore you can add the following lines in your ~/.bash_profile to load it:
# Load user profile file
if [ -f ~/.profile ]; then
. ~/.profile
fi

My Bash aliases don't work

I'm not sure why but my Bash aliases don't seem to work. Here is my .bashrc file
# v 0.0.1 - 7/03/12
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
# expanding history to 10000 commands
export HISTSIZE=10000
# don't store repeated commands more than once
export HISCONTROL=ignoredups
# where to look for Java
export JAVA_HOME=/Library/Java/Home
# tomcat server configuration
export CATALINA_HOME=/usr/local/apache-tomcat-6.0.35
# default editor
export EDITOR=vim
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
Here is my .bash_aliases file
# v 0.0.1 - 7/03/12
# aliases for directory traversal
alias ..='cd ../'
alias ...='cd ../../'
alias ....='cd ../../../'
alias gs='git status '
alias ga='git add '
alias gb='git branch '
alias gc='git commit'
alias gd='git diff'
alias go='git checkout '
alias gk='gitk --all&'
alias gx='gitx --all'
alias got='git '
alias get='git '
Add this to the end of your .bashrc:
if [ -f $HOME/.bash_aliases ]
then
. $HOME/.bash_aliases
fi
I had a similar problem recently. The solution appeared to be closing ALL open shells (root and user; I didn't notice that I was running a minimized root shell at the time while editing my user .bashrc and .bash_aliases files). The .bash_aliases file then seemed to get read.
By default
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
These are available in your .bashrc file in ubuntu 18,19
Actually the problem is sourcing the files, therefore source both files by runing the commands below. I faced the same issues and that is how i solved it.
source ~/.bashrc
source ~/.bash_aliases
Bash doesn't look for a file called .bash_aliases; you have to source it explicitly.
Looking around a bit, it appears ~/.bash_aliases is sourced from the default .bashrc on Ubuntu boxes; I don't have access to one to confirm. However, it is not a standard bash configuration file.
I recently installed RVM and changed my terminal profile to "run command as login shell". This disabled .bashrc from loading.
Fix: edit -> profile preferences -> Title and Command -> Run command as a login shell (uncheck)
Find this post for more information, fixed it for me.
https://askubuntu.com/questions/161249/bashrc-not-executed-when-opening-new-terminal
Sometimes forgetting to source the bashrc also creates this problem. So after adding your aliases don't forget to source it.
source ~/.bashrc
You need to include the file. Example code to do so from a default .bashrc file is below:
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
don't forget to
chmod 600 ~/.bash_aliases
on kubuntu 22.04
;)
It may be something simple. Like you are actually running zsh or korn instead of bash. Check your shell. I have done this on installing and testing various flavors. Wasted so much time I now never assume I am on bash anymore.

Resources