Correctly formatting a PYTHONPATH in my .bash_profile - bash

I would like to add a PYTHONPATH to my .bash_profile but would like to check I'm doing this the correct way. My .bash_profile (without a PYTHONPATH) looks like:
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin:/home/user/condor/bin:/home/user/merlin/bin
export PATH
The path I would like to add to my PYTHONPATH is:
/home/user/merlin/bin/strats/
Therefore would my updated .bash_profile (with PYTHONPATH) looks like:
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin:/home/user/condor/bin:/home/user/merlin/bin
export PATH
export PYTHONPATH=/home/user/merlin/bin/strats/
How can I correctly format this?

If it's your wish to be the sole owner and decision maker regarding PYTHONPTAH environment variable content on your interactive login shells, you're doing it right:
~/.bash_profile or ~/.profile
export PYTHONPATH=/home/user/merlin/bin/strats/
If you'd like to inherit any system-wide setting for PYTHONPATH environment variable, then you should:
~/.bash_profile or ~/.profile
export PYTHONPATH=$PYTHONPATH:/home/user/merlin/bin/strats/
Be aware that if you're working in a system where you can launch new terminals without logging in (i.e: launching a new xterm on your linux desktop), or in case you need that specific environment variable to run a script via cron, .bash_profile won't be executed and therefore the environment variable won't be available to that script.
As discussed in this answer comments, you can choose to use the ./~profile file instead of ~/.bash_profile to have additional compatibility with other shells.
Some folks simply add all environment configuration in ~/.bashrc. Since your .bash_profile template call ~/.bashrc, you'd end up having those environment variables available in interactive login and non-login shells.
For scripts that run via cron, you should directly source the file where you have your environment configuration on the script itself or on the cron line because that won't be done automatically for you (crontab launches non-interactive shells to run the scripts and these are not affected by ~/.bashrc, ~/.bash_profile or ~/.profile).

Related

Why do my $PATH environment always reset after I open a new terminal on my mac?

Usually I will nano .zsh_profile
Then I will edit the path
#PYTHON
export PATH=/Users/ffff/Library/Python/3.8/bin:$PATH
# JAVA
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH
#ANDROID
export ANDROID_HOME=/Users/ffff/Library/Android/sdk
export PATH=$ANDROID_HOME/platform-tools:$PATH
export PATH=$ANDROID_HOME/tools:$PATH
export PATH=$ANDROID_HOME/tools/bin:$PATH
then I will save and exit
Then I will
source .zsh_profile
I will test the Java and ADB all is good, but one I open a new Terminal from my mac, it will say ADB and JAVA and Android_HOME not found
Why is it not persist? Did I miss out anything? My mac version is 12.2.1
The .zsh_profile file, has no special meaning. This is probably a confusion form Bash, where the bash_profile is executes for login shells. In Zsh, that equivalent would be ~/.zprofile. You might have some code in your .zshrc file, that overrides the PATH variable with something else. Because, as oppsed to .zprofile, the .zshrc file gets executed for every interactive non-login shells.
So my advice, checkout .zshrc and see if there's something overriding the PATH there, if so, maybe you want to change that, and NOT to execute these commands you want to add for every time a shell is opened, you should put them in .zprofile, so they only get executed once at login.

The /etc/environment file is not executing certain commands

Given below are the contents of my /etc/environment file
alias ...="cd ../../"
alias ls="ls -al"
export blah="blah blah"
When I start new terminal session and change to sudo user as sudo su, only the export command has run, which I am able to verify using env. The aliases are not set.
If I run source /etc/environment the aliases get set as expected. Am I missing something? I also read that /etc/environment is only read when the system boots. Is that true?
I am running on RHEL 7.
The /etc/environment is intended for setting environment variables for every user on login. Therefore you don't need to use export in this file.
Adding alias into this file won't work, because this file is not a shell script and only accepts variable=value pairs.
/etc/environment is used by the PAM-env module and is agnostic to
login/non-login, interactive/non-interactive and also Bash/non-Bash,
so scripting or glob expansion cannot be used. The file only accepts
variable=value pairs.
It's not possible to export aliases or set them globally - they need to be set again in every shell instance.
The file you want to use is ~/.bashrc in a home directory of a user. This file gets executed every time a user opens a bash shell. So aliases and variables set in this file will have effect only on that shell.
You can also use /etc/bash.bashrc which is System-wide .bashrc file for interactive bash shells.
The reason why the export in your /etc/environment worked and actually created and env variable is that the pam-env parser specifically ignores export keyword to avoid confusion for people who don't know that /etc/environment is not a shell script.
You can see that in pam_env.c source code
/* skip over "export " if present so we can be compat with
bash type declarations */
if (strncmp(key, "export ", (size_t) 7) == 0)
key += 7;
Its available for example here - Linux-PAM/pam_env.c v0.79. See line 00234.

How can I define my text editor in bashrc?

I'm trying to set a variable containing my editor in ~/.bashrc. Unfortunately it doesn't seem to be coming through
# ~/.bashrc
export EDITOR=sublime
in terminal:
source ~/.bashrc
echo $EDITOR
=> nothing
How can I set and persist this variable?
Edit
This is my current .bashrc file:
source ~/.profile # Get the paths
source ~/.bashrc # get aliases
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
### Added by the Heroku Toolbelt
export PATH="/usr/local/heroku/bin:$PATH"
export EDITOR=sublime
Depending on your OS, check your .bash_profile or /etc/bash.bashrc
If your .bashrc isn't sourced, your .bashrc isn't read. All you need to do is source your file or drop your export command in something that is.
The .bashrc is only read in when a new shell is created and not when you log in. If you want your .bashrc read in during logins, you have to add this line to your $HOME/.bash_profile or $HOME/.profile:
[ -x $HOME/.bashrc ] && . $HOME/.bashrc
Note that $HOME/.bashrc must be both readable and executable by the user. (i.e., the file permission must be 5.. or 7..) for it to work. Make sure that your file permissions are set correctly.

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

Force base_profile to run

When I login to a sun box: SunOS domain.com 5.8 Generic_117350-57 sun4u sparc SUNW,Sun-Fire-V240
I start in the sh shell: SHELL=/bin/sh
I type bash to start a bash shell, then have to type . .bash_profile to load my profile. Is there a way it can be set to automatically load the profile?
Put
. ~/.bash_profile
in ~/.bashrc (watch out for infinite loops!). I don't use that myself, as I have most stuff in .bashrc to begin with (except, well, env. stuff and so on).
.bash_profile is only used for login shells, for non-login shells (like yours), bash uses .bashrc.
The easiest thing is for you to add . .bash_profile to your .bashrc.
You also have to make sure that you don't print out anything in non-interactive shells, or you can break scp/sftp.
Running bash like this causes it to source /etc/bash.bashrc and then ~/.bashrc in lieu of .bash_profile (and other files). Therefore, put . ~/.bash_profile in ~/.bashrc, (or in /etc/bash.bashrc to do this for all users).
The file .bash_profile is run when upon login. The file .bashrc is run when running bash from a non-login session.
Try adding this to your .bashrc:
source ~/.bash_profile

Resources