Run .bash_profile when I open a new terminal - macos

When I open a new terminal, I have to run :
source ~/.bash_profile every time...
How can I automate this ?
I have read lot of thing on internet but I don't find any clear response.
Thanks
FYI : when I run
echo $SHELL
I get /bin/zsh

As your shell is zsh, it loads .zshrc file when terminal (in login mode) started.
Either you have to change your shell to be /bin/bash or copy .bash_profile into .zshrc
to change the shell:
chsh -s /bin/bash
to copy profile:
cp ~/.bash_profile ~/.zshrc
# or create a symlink
ln -s ~/.bash_profile ~/.zshrc

As zsh terminal load .zshrc when started.
So we can loading .bash_profile actively by
echo 'source ~/.bash_profile' >> ~/.zshrc

Related

zsh profile path is not reflected.. in terminal in Mac OS 11.2.3

I have installed mysql, trying to set path.
but somehow it does not reflect.
Is not Mysql compatible for zsh by any chance?
% brew list| grep mysql
mysql#5.7
echo 'export PATH="/usr/local/opt/mysql#5.7/bin:$PATH"' >> ~/.bash_profile
% echo 'export PATH="/usr/local/opt/mysql#5.7/bin:$PATH"' >> ~/.zshrc
% source ~/.bash_profile
then
% mysql --version
zsh: command not found: mysql
You're using zsh in your terminal, so you need to add export PATH="/usr/local/opt/mysql#5.7/bin:$PATH" into your ~/.zshrc file.
If you don't have that file, do touch ~/.zshrc then add that to your Zsh config file.
Alternatively, if you want to switch back to Bash, just do chsh -s $(which bash) (without sudo to only take effect on current logged-in user) and then restart your shell.

oh-my-zsh does not start on mac

I installed oh-my-zsh as suggested in http://ohmyz.sh/.
FYI,
[~]$ zsh --version
zsh 5.0.8 (x86_64-apple-darwin15.0)
[~]$ echo $SHELL
/bin/zsh
[~]$ ls .oh-my-zsh/
LICENSE.txt cache lib oh-my-zsh.sh templates tools
README.md custom log plugins themes
Only when I type "zsh" then I can see the oh-my-zsh prompt like:
[~]$ zsh
➜ ~
Also, I tried to change my default shell to zsh:
[~]$ chsh -s /bin/zsh
Changing shell for myUserName.
Password for myUserName:
chsh: no changes made
How can I use directly oh-my-zsh whenever I start mac terminal?
Not only in System Pref>User and Group>...>User profile's shell settings, but also in Mac's terminal>Preference>ChosenProfile>Shell section, I had to change /bin/bash to /bin/zsh. after that, it works! Thank you, #RemyJ!
You have to change the default shell to zsh.
To do so use the following command.
chsh -s /bin/zsh
restart your terminal
I took it from here.
These were the steps I followed when I installed zsh.
I hope it will help you as well.
Run the following in the terminal:
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
Also, I tried to change my default shell to zsh:
[~]$ chsh -s /bin/zsh
try
chsh -s $zsh
that worked fine for me
I had a similar issue but I resolved it by executing the command chsh -s /bin/zsh in the terminal,then navigated to
Terminal -> Preferences -> General window
and changed the Shells open with: option to Default login shell while the command (complete path) set to /bin/zsh.This should solve your problem.

Archlinux + MATE Terminal - `.bash_profile` is not being sourced

I am using Arch Linux with MATE as desktop environment. So terminal emulator is MATE Terminal. Recently I installed Jekyll with gem install jekyll. But when I ran jekyll -v it says bash: jekyll: command not found. So I tried to add path of Jekyll to PATH variable.
I ran PATH=$PATH/$HOME/.gem/ruby/2.2.0/bin and it worked perfectly. Now I can run jekyll commands. To add it permanently to PATH variable I edited the ~/.bash_profile file like following. It is not working after reboot. But
source ~/.bash_profile works perfectly.
#
# ~/.bash_profile
#
[[ -f ~/.bashrc ]] && . ~/.bashrc
export PATH="${PATH}:/home/heisenberg/.gem/ruby/2.2.0/bin"
According to ArchWiki this is the proper way to concat something permanantly to PATH. But it isn't working. Can somebody figure me out where the wrong is?
[N. B. : Adding the same line in ~/.bashrc is doing okay.]
Depending on the option it is given, bash can be run as an interactive shell or login shell. The default interactive shell mode does not read ~/.bash_profile. login shell bash do.
See:
First, some setup:
% cat ~/.bashrc
…
export BASHRC="yes"
…
% cat ~/.bash_profile
…
export BASH_PROFILE="yes"
…
Now run a regular (interactive) bash:
% bash
[galaux#magenta ~]$ echo $BASHRC
yes
[galaux#magenta ~]$ echo $BASH_PROFILE
Notice we did not get yes with this last one.
Now with a login shell:
% bash --login
[galaux#magenta ~]$ echo $BASHRC
yes
[galaux#magenta ~]$ echo $BASH_PROFILE
yes
See paragraph INVOCATION from man bash.

How to run .profile inside a shell script in ubuntu

I am running a script which is echoing the environment variables in .profile file and than I am running the script, but I am getting following error
I tried following:
node#node-virtual-machine:~$ cat env.sh
#!/bin/bash
echo 'export JAVA_HOME=/home/node/jdk1.6.0_45' >> /home/node/.profile
echo 'export PATH=$PATH:$JAVA_HOME/bin' >> /home/node/.profile
cd /home/node/
source .profile
node#node-virtual-machine:~$ sh env.sh
sudo: source: command not found
How to execute .profile within a script?
Instead of:
sh env.sh
You should run:
bash ./env.sh
Besides instead of:
source .profile
use:
source ~/.profile
If .profile exists as /home/node/.profile, you have all that is necessary.
Your script says /bin/bash at the top but you run it with sh which is probably dash on your system. You probably are already running Bash at the prompt, so you should say source env.sh instead of sh env.sh if you want the variables to be exposed to your terminal.

MacOSX how to autorun a script after su - root?

Lets assume i am normal user, the i will switch to root:
user ~ $ su - root
Password:
root ~ #
So once i logged in as root, i want to run following command automatically:
source .bash_profile
How can i have that above command run automatically please?
According to the bash man page, .bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.
In your case, you don't need to source .bash_profile like this.
You just need to put source .bash_profile in your root's .bashrc file
if [ -f ~/.bash_profile ]; then
source ~/.bash_profile
fi
Read me for better understanding of .bash_profile and .bashrc
Update
Example:
[root#sgeorge-ld ~]# cat .bashrc | tail -1
echo "Testing .bashrc for a stack query"
[root#sgeorge-ld ~]# exit
logout
[sgeorge#sgeorge-ld ~]$ su - root
Password:
Testing .bashrc for a stack query
[root#sgeorge-ld ~]#
First of all, when you switch to root user, you will be still your regular user's home directory. Which .bash_profile you want to execute? /Users/myuser/.bash_profile or root's /var/root/.bash_profile?
Regardless of what you would like to execute, you can edit /var/root/.bashrc (if you don't have it, create one) and add your command there.

Resources