SH File not setting ENV variables (Mac OS X) - macos

I have a file set_env.sh, that I run in the terminal using:
sh set_env.sh
The code runs, but the variables are not set. I am checking with
echo $EMAIL_SERVER
set_env.sh
#!/bin/sh
echo email Address
read y
echo email password?
read x
export EMAIL_SERVER='smtp.gmail.com'
export EMAIL_PORT=587
export EMAIL_DOMAIN='domain.com'
export EMAIL_AUTHENTICATION='plain'
export EMAIL_ENABLE_STARTTLS_AUTO=true
export EMAIL_USERNAME=$y
export EMAIL_PASSWORD=$x

Use source command instead:
source set_env.sh
The command executes the script in the current shell context as opposed to invocation via separate shell process: sh set_env.sh (the environment variables are set within the new sh process which is isolated from the current process).
By the way, you can use dot (.) instead of source, if you prefer.

You may modify your ~/.bash_profile file in your home dir with this
export EMAIL_SERVER='smtp.gmail.com'
export EMAIL_PORT=587
export EMAIL_DOMAIN='domain.com'
export EMAIL_AUTHENTICATION='plain'
export EMAIL_ENABLE_STARTTLS_AUTO=true
export EMAIL_USERNAME=$y
export EMAIL_PASSWORD=$x

Related

Trying to install hadoop on a Linux system, getting error while editing .bashrc file

I am installing hadoop in my system while trying to make permanent .bashrc changes using --source ~/.bashrc getting the error below:
/home/tcs/hadoop>source ~/.bashrc
ksh: .[5]: .[35]: shopt: not found [No such file or directory]
ksh: .[5]: .[46]: shopt: not found [No such file or directory]
ksh: .[5]: .[65]: [: argument expected
the .bashrc file content is
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User specific aliases and functions
export JAVA_HOME=/usr/lib/jvm/jre-1.6.0-openjdk.x86_64
export HADOOP_INSTALL=/home/tcs/hadoop
export PATH=$PATH:$HADOOP_INSTALL/bin
export PATH=$PATH:$HADOOP_INSTALL/sbin
export HADOOP_MAPRED_HOME=$HADOOP_INSTALL
export HADOOP_COMMON_HOME=$HADOOP_INSTALL
export HADOOP_HDFS_HOME=$HADOOP_INSTALL
export YARN_HOME=$HADOOP_INSTALL
export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_INSTALL/lib/native
export HADOOP_OPTS="-Djava.library.path=$HADOOP_INSTALL/lib"
There should be a respective rc file for ksh that you need to edit instead.
And if you find it, note that bash and ksh have slightly different syntaxes, but that shouldn't be an issue if you are only exporting environment variables
Also, latest versions of Hadoop do not support Java 6, so you'll need to update the Java home variable
It seems you run ksh (Korn-shell) environment. According to the .kshrc:
The $HOME/.kshrc file is a shell script that customizes the Korn-shell
environment. This .kshrc script often contains a list of environment
variables, command aliases, and function definitions that customize
the Korn-shell environment.
You can add your EXPORT statements to the .kshrc file as they are. The issue you see is with the script located in the /etc/bashrc file.
Or as an alternative, use bash instead. Simply run /bin/bash for a one time use. Or set it up as a default shell via chsh -s /bin/bash. You must log out and log back in to see this change.

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.

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.

How can I make export permanent?

If I set a variable using export and then get out of the command line and go back to it, it no longer has that value. This is because export only works for subprocesses but not for parent processes. How can I get export to make the value permanent?
You can add the export in the file $HOME/.bash_profile and then run the following command:
source $HOME/.bash_profile

Resources