How can I define my text editor in bashrc? - bash

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.

Related

Correctly formatting a PYTHONPATH in my .bash_profile

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).

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...

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.

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