Exporting environment variables in ~/.bash_profile produces wrong values - macos

On my MacBook with MacOS Catalina I try to export multiple environment variables by setting them in the ~/.bash_profile file. I proceed as follows:
touch ~/.bash_profile
open ~/.bash_profile
Then the file ~/.bash_profile opens in my text editor and I add multiple environment variables:
export VAR1="some_value"
export VAR2="some_value"
export VAR3="some_value"
export VAR4="some_value"
export VAR5="some_value"
Then I save the file and apply the changes as follows:
source ~/.bash_profile
Now I should be able to access the values of the newly defined variables. If I type echo ${VAR1} or echo ${VAR2} I get the correct value some_value, but if I type echo ${VAR3} I get export$lue, i.e. the first seven characters are replaced by export$.
Any advice?

Related

Where is Shell Path in mac. .bash_profile Dont works correctly

When i use:
nano .bash_profile
the terminal show me:
export PATH=/usr/bin:/usr/sbin:/bin:/usr/local/bin:/sbin:/opt/x11/bin:$PATH
# Setting PATH for Python 3.4
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
export PATH
export PATH="/Applications/MAMP/Library/lib"
export PATH="/Applications/MAMP/Library/bin"
#XAMPP
#export PATH="/Applications/XAMPP/xamppfiles/bin:$PATH"
#export PATH="/Applications/XAMPP/xamppfiles/lib:$PATH"
But when i open a new terminal only shows. echo $PATH prints.
/Applications/MAMP/Library/bin
The problem is that when i execute a command the terminal returns :
command not found
I need to execute this command for the terminal to operate properly
export PATH=/usr/bin:/usr/sbin:/bin:/usr/local/bin:/sbin:/opt/x11/bin:$PATH
and echo $PATH prints.
/usr/bin:/usr/sbin:/bin:/usr/local/bin:/sbin:/opt/x11/bin:/Applications/MAMP/Library/bin
What can i do to open and edit the correct shell PATH ?
You are overiding your PATH variable every time you export. your .bash_profile should be
export PATH=/usr/bin:/usr/sbin:/bin:/usr/local/bin:/sbin:/opt/x11/bin
export PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
export PATH="/Applications/MAMP/Library/lib:$PATH"
export PATH="/Applications/MAMP/Library/bin:$PATH"
this way your current path is added to the end of the new path variable

Java_HOME not found when changed shell from Bash to Zsh on OSX?

This is weird, I have set JAVA_HOME for my mac which can be found when I am using bash shell, but if I change shell, I get a message saying JAVA_HOME not set. What could be going on here?
I stumbled upon your question when trying to solve the same issue while migrating from bash to oh-my-zsh. The reason it's not there is that there is no code setting it for zsh but there was for bash. Generally theres something exporting JAVA_HOME whenever a new bash window is opened so it's always set for you. There is a good thread where this might be happening on the Unix & Linux StackExchange site.
To do the same thing in zsh, you can edit the .zshrc which is run every time zsh starts. I found a sample .zshrc which got me most of the way. The key line being:
export JAVA_HOME=`/usr/libexec/java_home`
Here is the file which I appended to the end of my existing ~/.zshrc file:
#zshrc, interactive shell settings
export ZSH=$HOME/.zsh
# emacs integration
[[ $EMACS = t ]] && unsetopt zle
# env
if [[ -e /usr/libexec/java_home ]]; then
export JAVA_HOME=`/usr/libexec/java_home`
fi
if [[ -e /usr/local/lib/node_modules ]]; then
export NODE_PATH=/usr/local/lib/node_modules
fi
# path
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin
export PATH=/opt/usr/sbin:/opt/sbin:/opt/usr/bin:/opt/bin:$PATH
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
export PATH=$HOME/.cabal/bin:$PATH
export PATH=$HOME/.gem/ruby/1.8/bin:$PATH
export PATH=$JAVA_HOME/bin:$PATH
export PATH=$HOME/.bin:$PATH
setopt null_glob
# source all files in zsh root
for include in $ZSH/*.zsh; do
source $include
done
# source all non-controlled files
for include in $ZSH/private/*.zsh; do
source $include
done
unsetopt null_glob
Then source ~/.zshrc to run in the current shell (or just start a new one) and you should be able to see that it is set with export | grep JAVA_HOME.
I also ended up running mkdir ~/.zsh to create the directory this is looking for and removing the .cabal and .gem lines as they were not needed for me.
I have just installed Mac OS Catalina Version 10.15 and found that environment variables such as JAVA_HOME and others that have been set in my .bash_profile :
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
export ANDROID_HOME=/Users/mynziak/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
export M2_HOME=/usr/local/Cellar/maven/3.6.2/libexec
export M2=${M2_HOME}/bin
export PATH=${PATH}:${M2_HOME}/bin
are not set in fact!
I saw % in terminal instead of general $ that means you are using a zsh shell instead of bash shell. With Catalina zsh is now the default shell and bash will be completely gone in the future.
oh-my-zsh shell:
https://ohmyz.sh/
So you have to setup all environment variables in .zshrc file.
I just copy-pasted every variables from .bash_profile in to .zshrc and re-opened terminal.
Files .bash_profile and .zshrc are hidden (cmd+shift+. - show hidden files in finder) but can be found in path:
/Users/mynziak/.zshrc
but use own username!
When you set JAVA_HOME in a shell, then it is active and available only for that context, and it will be gone when you close that shell.
Instead either change global environment (or) your .bashrc to include it. So that every time you start a shell, the variable will be available.
edit the .profile or .bash_profile to include the JAVA_HOME.
export JAVA_HOME=`/usr/lib....`
and also below command will return the path for java home directory.
/usr/libexec/java_home -v 1.7
where 1.7 is the version you want.
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
export PATH=$JAVA_HOME/bin:$PATH
Add above 2 lines in ~/.bashrc or ~/.zshrc and reload the file using source command.

-bash: : command not found when launching the shell in Mac OSX

Lately I tried to made some changes to paths in my .bash_profile.
After I tried to validate these changes with source .bash_profile and got -bash: : command not found, I have also noticed that when I open the shell, before the execution of any command, it stands already "-bash: : command not found"...
Obviously, changes made to .bash_profile weren't taken into consideration by the system niether...
Please, help! I guess that I messed with the .bash_profile too much, have no idea how to fix it...
Contents of .bash_profile:
cat .bash_profile
# Finished adapting your PATH environment variable for use with MacPorts.
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
##
# MacPorts Installer addition on 2014-03-24_at_12:05:55: adding an appropriate PATH variable for use with MacPorts.
### Your previous /Users/usrname/.bash_profile file was backed up as /Users/usrname/.bash_profile.macports-saved_2014-03-24_at_12:05:55
##export PATH="/Users/usrname/anaconda/bin:$PATH"
# added by Anaconda 1.9.1 installer
##Additional binary folders
export PATH=/Users/usrname/TreeTagger/cmd:$PATH
export PATH= /Users/usrname/TreeTagger/bin:$PATH
export UIMA_HOME= $/Users/usrname/apache-uima
export PATH=$PATH:$UIMA_HOME/bin
export HEIDELTIME_HOME='/Users/usrname/Downloads/heideltime-kit/'
export TREETAGGER_HOME='/Users/usrname/TreeTagger'
source $HEIDELTIME_HOME/metadata/setenv
Updated contents of .bash_profile:
cat .bash_profile
# Finished adapting your PATH environment variable for use with MacPorts.
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
##
# MacPorts Installer addition on 2014-03-24_at_12:05:55: adding an appropriate PATH variable for use with MacPorts.
### Your previous /Users/usrname/.bash_profile file was backed up as /Users/usrname/.bash_profile.macports-saved_2014-03-24_at_12:05:55
##export PATH="/Users/usrname/anaconda/bin:$PATH"
# added by Anaconda 1.9.1 installer
##Additional binary folders
export PATH="/Users/usrname/TreeTagger/cmd:$PATH"
export PATH="/Users/usrname/TreeTagger/bin:$PATH"
export UIMA_HOME="/Users/usrname/apache-uima"
export PATH=$PATH:$UIMA_HOME/bin
export HEIDELTIME_HOME="/Users/usrname/Downloads/heideltime-kit/"
export TREETAGGER_HOME="/Users/usrname/TreeTagger"
source $HEIDELTIME_HOME/metadata/setenv
Full shell output after last quotes' correction:
Last login: Thu Jul 31 19:07:05 on ttys000
-bash: : command not found
usr:~ usrname$ cat .bash_profile
# Finished adapting your PATH environment variable for use with MacPorts.
export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
##
# MacPorts Installer addition on 2014-03-24_at_12:05:55: adding an appropriate PATH variable for use with MacPorts.
### Your previous /Users/usrname/.bash_profile file was backed up as /Users/usrname/.bash_profile.macports-saved_2014-03-24_at_12:05:55
##export PATH="/Users/usrname/anaconda/bin:$PATH"
# added by Anaconda 1.9.1 installer
##Additional binary folders
export PATH="/Users/usrname/TreeTagger/cmd:$PATH"
export PATH="/Users/usrname/TreeTagger/bin:$PATH"
export UIMA_HOME="/Users/usrname/apache-uima"
export PATH="$PATH:$UIMA_HOME/bin"
export HEIDELTIME_HOME="/Users/usrname/Downloads/heideltime-kit/"
export TREETAGGER_HOME="/Users/usrname/TreeTagger"
source $HEIDELTIME_HOME/metadata/setenv
usr:~ usrname$ source .bash_profile
-bash: : command not found
Edit with echo commands:
cat .bash_profile
# Finished adapting your PATH environment variable for use with MacPorts.
export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
##
# MacPorts Installer addition on 2014-03-24_at_12:05:55: adding an appropriate PATH variable for use with MacPorts.
### Your previous /Users/usrname/.bash_profile file was backed up as /Users/usrname/.bash_profile.macports-saved_2014-03-24_at_12:05:55
##export PATH="/Users/usrname/anaconda/bin:$PATH"
# added by Anaconda 1.9.1 installer
##Additional binary folders
export PATH="/Users/usrname/TreeTagger/cmd:$PATH"
export PATH="/Users/usrname/TreeTagger/bin:$PATH"
export UIMA_HOME="/Users/usrname/apache-uima"
export PATH="$PATH:$UIMA_HOME/bin"
export HEIDELTIME_HOME="/Users/usrname/Downloads/heideltime-kit/"
export TREETAGGER_HOME="/Users/usrname/TreeTagger"
echo before
source $HEIDELTIME_HOME/metadata/setenv
echo after
usr:~ usrname$ source .bash_profile
-bash: : command not found
usr:~ usrname$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin
Edit +bash version:
echo $BASH_VERSION
3.2.51(1)-release
usr:~ usrname$ source ./.bash_profile
-bash: : command not found
Edit +after set -x was added to .bash_profile:
usr:~ usrname$ source ./.bash_profile
++ $'\016'
-bash: : command not found
++ update_terminal_cwd
++ local 'SEARCH= '
++ local REPLACE=%20
++ local PWD_URL=file://usr.local/Users/usrname
++ printf '\e]7;%s\a' file://usr.local/Users/usrname
This line:
export PATH= /Users/usrname/TreeTagger/bin:$PATH
is the problem. In a variable assignment, including an export, you can't have spaces around the =. With the added space, it sets $PATH to the empty string.
Change it to:
export PATH=/Users/usrname/TreeTagger/bin:$PATH
For safety, you should add double quotes:
export PATH="/Users/usrname/TreeTagger/bin:$PATH"
just in case there are any special characters in the value of $PATH.
You have the same problem with UIMA_HOME.
UPDATE :
Based on later comments, you appear to have a stray control character in your .bash_profile. With set -x it's displayed as $'\016'. bash tries to treat that as a command name, and not surprisingly is unable to find it. Edit your .bash_profile and delete that character.
(The other errors needed to be corrected anyway.)

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.

Unable to change the default editor in the terminal

My default editor is Pico at my server. I use Bash and Linux.
I tried to change Vim to be my default editor unsuccessfully by:
echo vim > $EDITOR
How can I change Vim to be my default editor?
The following code does not work in file .bashrc:
export EDITOR='vim'
Adding
export EDITOR=vim
to your .bashrc file should really do the trick. (Quotes aren't necessary there and, depending on what quotes you used, they may be the cause for your problem.)
You must open a new shell (or enter source ~/.bashrc at the prompt) after modifying file .bashrc for the modification to take effect.
What is the program from which you want Vim to be started?
I haven't used Git, but the documentation reads:
The editor used to edit the commit log message will be chosen from the GIT_EDITOR environment variable, the core.editor configuration variable, the VISUAL environment variable, or the EDITOR environment variable (in that order).
So check whether one of these variables is set:
echo $GIT_EDITOR $VISUAL $EDITOR
git config --get-all core.editor
For me,
export VISUAL=vim
solved the problem.
You can use the Git configuration option core.editor to set the editor of your liking, e.g., nano:
git config [--global] core.editor "nano"
You can also change this by editing the .gitconfig file in your home directory (global) or git repository (create it if it doesn't exist) if you don't have shell access:
...
[user]
name = Your Name
email = your#email.address
[core]
editor = nano
...
Check this command:
sudo update-alternatives --config editor
vim=/usr/bin/vim # Or wherever the Vim binary is
export EDITOR=vim
should do the job.
I don't have an EDITOR environmental variable. My .bashrc file does define this:
alias vi='vim'
And supposedly, if Vim can't find a file called .vimrc in your home directory, it runs in "compatibility mode" and you only get vi features until you say type :nocp.
If it is based on your EDITOR environmental variable, you would set it like this in Bash:
export EDITOR='vim'
Since things have changed in Mac OS X, you will have to add the following in the .profile file in the base directory of the user:
export EDITOR='vim'
You can follow the following instructions:
open the terminal
Type cd (hit Return or Enter (this will take you to the base directory))
Type echo "export EDITOR='vim'" >> .profile (hit Return or Enter and you are done)
(Restart the terminal)
=========================
Or just type:
echo "export EDITOR='vim'" >> ~/.profile
Hit Enter and restart.
If you want vi to be your default history editor (which is why I'm here):
Edit file ~/.bashrc and add
set -o vi
anywhere in the file. Then all the lovely vi command history is available (Esc + K, etc.).
Since none of these answers are helping me:
Here is what the Git documentation are saying, git-commit(1) Manual Page:
The editor used to edit the commit log
message will be chosen from the
GIT_EDITOR environment variable, the
core.editor configuration variable,
the VISUAL environment variable, or
the EDITOR environment variable (in
that order).
Here is the Bash man page excerpt on export (brackets are optional):
export [-fn] [name[=word]]
I had this same challenge when setting up my new MacBook Pro.
Here's how I solved it
To switch to your editor of choice (say nano) on a MacBook you will need to add the following lines to your ~/.zshrc file if your default shell is zsh or ~/.bash_profile if your default shell is bash:
export EDITOR=nano
export VISUAL="$EDITOR"
However, a simpler approach to do this will be to use the echo command to insert them into your ~/.zshrc file if your default shell is zsh:
echo 'export EDITOR=nano' >> ~/.zshrc
echo 'export VISUAL="$EDITOR"' >> ~/.zshrc
OR ~/.bashrc if your default shell is bash:
echo 'export EDITOR=nano' >> ~/.bash_profile
echo 'export VISUAL="$EDITOR"' >> ~/.bash_profile
Run the command below to activate the new configuration:
source ~/.zshrc
Or
source ~/.bash_profile
If you need to switch to other editors of choice you can replace nano with your preferred editor:
Vim - vim
Vi - vi
That's all.

Resources