Trying to understand Bash file setups/structure - bash

This is an extension of a previously asked question: What are some common HDFS commands that can be mapped in the bash files?
I notice that the .bashrc and .bash_profile that I was initially provided are slightly different that what you have provided. Is this OK or some kind of different pattern?
The files that were copied over when I started are as follows:
.bashrc
.bash_profile
.bashrc
source /etc/bashrc
...and a lot of other folder mappings
.bash_profile
# .bash_profile
# Get the aliases and functions
#if [ -f ~/.bashrc ]; then
# . ~/.bashrc
#fi
source ~/.bashrc
I created the .bash-aliases file as you recommended.
.bash_aliases
alias h="hdfs dfs"
I have modified the .bashrc file as follows
.bashrc - Modified
source /etc/bashrc
...and a lot of other folder mappings
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

The .bashrc code will be executed each before a new shell is started. The code that is in there doesn't matter, as long as it is valid bash.
The other files (bash_aliases) are just here to separate the commands in different files.
This article explains it quite well: https://ss64.com/bash/syntax-bashrc.html
So to answer your question, it won't cause problems at all. All that matters is that the way it's done satisfies you.

Related

Conda does not set paths when activating environment

When starting a new shell, the PATH environment variable is not configured
properly. The directories anaconda3/bin or miniconda3/bin are at second
position only, not at first position in the PATH variable. This can be
resolved by conda deactivate and activating it again.
This question was asked several times already (e.g. here and here)
but the existing questions are either very old or concentrate on the use of
source activate env-name. All in all, I found no answer that resolves my
problem.
When I start a new shell, the base environment is activated. The relevant
snippet from my .bashrc reads like this:
condaexe="/home/$USER/.miniconda3/bin/conda"
condash="/home/$USER/.miniconda3/etc/profile.d/conda.sh"
__conda_setup="$($condaexe 'shell.bash' 'hook' 2> /dev/null)"
# shellcheck disable=SC2181
if [[ $? -eq 0 ]]
then
eval "$__conda_setup"
elif [[ -f "$condash" ]]
then
source "$condash"
fi
unset __conda_setup condaexe condash
Then, the PATH variables are defined as follows:
(base)$ echo $PATH
/home/user/.local/bin:/home/user/.miniconda3/bin:/home/user/.miniconda3/condabin:/home/user/workspace/my-project/:/home/user/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
(base)$ conda deactivate && echo $PATH
/home/user/.local/bin:/home/user/.miniconda3/condabin:/home/user/workspace/my-project/:/home/user/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
$ conda activate base && echo $PATH
/home/user/.miniconda3/bin:/home/user/.local/bin:/home/user/.miniconda3/condabin:/home/user/workspace/my-project/:/home/user/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
(base)$
Note that /home/user/.local/bin is contained twice; once before and once
after the Miniconda3 directories.
I tried to debug the problem by appending the following snippet to .bashrc:
echo $CONDA_PROMPT_MODIFIER
echo $PATH
This yields
(base)
/home/user/.miniconda3/bin:/home/user/.miniconda3/condabin:/home/user/workspace/my-project:/home/user/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
which would be perfectly fine but, somehow, is modified after .bashrc.
Note that here /home/user/.local/bin is contained only once.
What goes on here? How can I setup Bash/Conda to get a properly defined PATH
environment variable?
There are three components I missed in the original question which are key to the solution.
First, I run all my shells inside TMux. Second, TMux sources the .profile. Third, in .profile some local directories like the aformentioned ~/.local/bin are blindly prepended to the PATH.
Taken this altogether all the weird behaviour above makes sense. Especially that PATH is correct at the end of ~/.bashrc but not in the shell is obvious now; it got modified in between by ~/.profile.
There are three solutions for this:
deactivating and activating conda manually (temporary workaround)
being more picky about which shells are started inside TMux (very unhandy)
commenting out the manipulations of PATH in ~/.profile
It seems as if one cannot configure TMux to source only ~/.bashrc (reference 1, reference 2), albeit some workarounds exist here too.
This is related to the Bash init files. By default, ~/.bashrc is used in an interactive, non-login shell. It won't be sourced in a login shell. Tmux uses a login shell by default. Hence, shells started by tmux skip ~/.bashrc.
default-command shell-command
The default is an empty string, which instructs tmux to create a login shell using the value of the default-shell option.
Init files for Bash,
login mode:
/etc/profile
~/.bash_profile, ~/.bash_login, ~/.profile (only first one that exists)
interactive non-login:
/etc/bash.bashrc (some Linux; not on Mac OS X)
~/.bashrc
non-interactive:
source file in $BASH_ENV
The weird interactive, non-login loading requirement confuses people in other situations as well. The best solution is to change the loading requirement of ~/.bashrc as interactive only, which is exactly what some distros, like Ubuntu, are doing.
# write content below into ~/.profile, or ~/.bash_profile
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
This should be the solution you desire. And I recommend every Bash user setup this in the profile.
References
Unix Shell Initialization
man tmux
Thanks for this post! That give me hint to debug same issue. I think everyone is different situdation but the idea is same: there is a command like export $PATH change the path in your zshrc or .bashrc which will affect the computer to find the path you want.

How to source additional file when launching bash

Bash will source automatic profiles such as .bashrc. --rcfile option can be used to override the automatic script. But I need to source additional personalized file (that's the automatic script plus another file) when launching the bash shell without touching ANY files in $HOME or /etc directory since $HOME directory belongs to application run user. The personalized file must not be located in $HOME directory.
Is this possible?
I tried:
/bin/bash <<EOF
. /a-directory-outside-of-home/vanilla
EOF
but it returned to the current shell.
Okay, so you want to run the user's normal .bashrc, followed by your own script, and you want to trigger this behavior in the way that bash is called, correct?
The call:
/bin/bash --rcfile myscript
First line of myscript:
source $HOME/.bashrc
bash --rcfile <(cat rcfile1; cat rcfile2)
works just fine and requires no modifications anywhere.
Add your stuff as *.sh files in /etc/profile.d -- read /etc/profile and
http://www.gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files
I wouldn't modify the .bashrc file. Instead, I would modify the .bash_profile
file which is located at $HOME.
It is the place from where .bashrc is included.
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/bashrc
fi
A good option for you is to add a .personalized_settings file and include it
just below the above lines like this.
# Adding personalized settings
if [ -f ~/.personalized_settings ]; then
. ~/personalized_settings
fi
This requires that .personalized_settings file is located at $HOME.
I have tested the above in Fedora12.
Edit :
You might need to look for .profile instead of .bash_profile in Ubuntu (and hopefully other Debian based systems). (courtesy #Benjamin W.)
This (link)[https://www.gnu.org/software/bash/manual/bash.html#Bash-Startup-Files] will give you more information on it.
What if you mess around with any of the profile files?
You have a back-up copy of all these files in /etc/skel which you could
use to restore.

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.

Appending bash alias to .bashrc doesn't work

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.

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