Terminal doesn't start properly. bash command not found - macos

I have problems with my terminal after trying to install Homebrew on my Mac. When I start the terminal I get :
-bash: touch: command not found
When I do echo $PATH I get:
/usr/local/bin:??
I've tried several suggestions like
export PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin
and then it works, but when I restart my terminal, it starts all over.

According to the bash man page, .bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.
Mac OS X runs a login shell by default for each new terminal window, calling .bash_profile instead of .bashrc.
Setup your PATH variable in .bash_profile
vim ~/.bash_profile
correct PATH variable with below single line(which is working fine as per the question)
export PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin

Related

Issue with environment variable on Mac OS sierra

I am seeing a strange problem with the storing of an env in mac os.
I set custom env in ~/.bash_profile
export MYENV=user
Then ran the . ~/.bash_profile and then I printed the env using
printenv then I can see the MYENV=user in the list.
If I close the terminal and reopen and execute printenv then I could not see MYENV in the list still I can see the export MYENV=user in ~/.bash_profile. It seems strange to me.
I am using Mac os High Sierra 10.13.6.
Could some body please tell me what mistake I am doing?
Note that ~/.bash_profile is only run for login shells. From the man page:
When bash is invoked as an interactive login shell, or as a non-interactive shell
with the --login option, it first reads and executes commands from the file
/etc/profile, if that file exists. After reading that file, it looks for
~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and exe-
cutes commands from the first one that exists and is readable. The --noprofile
option may be used when the shell is started to inhibit this behavior.
So if you terminal isn't launching the shell with -l, --login or with $0 having a leading hyphen it won't be a login shell and thus won't read ~/.bash_profile. You may need to reconfigure how your terminal launches the shell if you want the shell to read that config script.
On the other hand ~/.bashrc is always read by an interactive shell. So if you put the export in that script it should do what you expect. It certainly does for me. You replied to Amila that it didn't work for you. So I'd suggest a simple experiment. Open two terminal windows. In one edit ~/.bashrc and add these two lines:
echo running .bashrc
export WTF=abc
In the other window just run bash. It should echo that message and echo $WTF should print abc. Now open a new terminal window. If you don't see that message and the env var isn't present then something is inhibiting reading that config script. Possibly the shell is being run with the --norc flag.
~/.bash_profile is executed before the initial command prompt is returned to the user, which means after a new login. Try adding the environment variable to ~/.bashrc instead.

How to make OS X to read .bash_profile not .profile file

I have read so many suggestions about, not putting your customization aka commands in ".profile" file. Rather, create a .bash_profile for yourself and add your alias and etc.
But,when I open the new terminal, if there is only .bash_profile, OS X is not exporting/sourcing the commands mentioned in it. I have to manually source the .bash_profile.
If I create .profile file, on opening a new terminal, all my commands in .profile are executed and will be available readily.
Could you please help me in understanding, how does it works? Also, when to use .bashrc/.profile/.bash_profile files.
Thanks!
According to Apple,
zsh (Z shell) is the default shell for all newly created user accounts, starting with macOS Catalina.
So you should verify your default shell with the command:
$ echo $SHELL
If the result is /bin/bash your default shell is BASH, and if the result is /bin/zsh the default is ZSH.
Go to home with $ cd ~/ and create the profile (if it does not exist) and edit it with the commands:
For bash:
$ touch .bash_profile
$ open .bash_profile
For ZSH:
$ touch .zprofile
$ open .zprofile
According to the manual page that ships with OS X:
... it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
It should only read ~/.profile as a last resort if neither ~/.bash_profile nor ~/.bash_login are readable.
On all of my OS X systems, I have my ~/.bash_profile set to:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
It is highly recommended that you do this on OS X in order to get bash to read your ~/.bashrc file like you would expect.
It's also possible that your terminal shell is defaulting to sh instead of bash. You can verify this first:
$ echo $SHELL
/bin/tcsh
To change this to bash, you can go into your Terminal -> Preferences -> Startup tab, and change "Shell Opens With:" from "Default login shell" to Command and value "/bin/bash".
Alternately, you can change your default shell by executing the following command at the command prompt:
chsh -s bin/bash
After you do one of these, open a new shell window, and your .bash_profile should be sourced.
For anyone else who finds this, instead of bash_profile, for new versions of mac you can use .zshrc. I.E., do
open .zshrc
and add what you need there.
You can use zsh to fix the problem.
The Z shell (also known as zsh) is a Unix shell that is built on top
of bash (the default shell for macOS) with additional features. It's
recommended to use zsh over bash.
Installation
Install zsh using Homebrew: $ brew install zsh
Install Oh My Zsh: $ sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
Move to .bash_profile setting .zshrc file
To apply the changes you make you need to either start new shell
instance or run: source ~/.zshrc
If you are using zsh, you can source to .bash_profile by adding the following line to .zprofile
if [ -f ~/.bash_profile ]; then
source ~/.bash_profile
fi
It should be mentioned that bash will first look for a /etc/profile file, as stated in the Bash man pages.
When bash is invoked as an interactive login shell, or as a non-inter-
active shell with the --login option, it first reads and executes com-
mands from the file /etc/profile, if that file exists. After reading
that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile,
in that order, and reads and executes commands from the first one that
exists and is readable. The --noprofile option may be used when the
shell is started to inhibit this behavior.
I solved by simply adding bash (in a newline) into ~/.bash_profile file.

Why do I need to source bash_profile every time

I have installed Hadoop and every time I want to run it, first I have to do this:
source ~/.bash_profile
or it won't recognize the command hadoop
Why is that?
I am on OSX 10.8
Now that we've narrowed down the problem:
Run ps -p $$ at the command line to check if you are, in fact, using a bash shell.
Realize that you are in zsh, which means you should be editing your profile in .zshrc.
Copy the offending lines from .bash_profile to .zshrc, OR
Modify your .zshrc to directly source your .bash_profile.
UPDATE: Do what #TC1 mentions in the comments and keep the shell-specific code in each shell's own profile, and from those profiles, only source shell-agnostic code.
On Mac Catalina, I just had to open "preferences" on terminal and change the "shells open with" from "default" to "Command(complete path)", which the default path was "/bin/zsh". touch ~/.zshrc, if that file doesn't exist already, and copy/paste your stuff from ".bash_profile" into the ".zshrc" file.
To elaborate, with terminal running, I opened "settings" from the Terminal menu on the Mac navbar. On the "General" tab, look for "Shells open with" select "Command (complete path)", and type in /bin/zsh.
bash_profile.sh is applicable for bash shell.
if your default shell is not bash and if your default shell is someother shell for example zsh then you have to manually load the .bash_profile using source ~/.bash_profile.
You can always change the default shell to bash shell so that the .bash_profile file will be automatically loaded.
Inorder to automatically load .bash_profile, you can update your default shell to bash using the command chsh -s /bin/bash
cat /etc/shells will list the default shells available in the
machine
echo $SHELL will display the currently active shell in your machine
To change active shell to a different shell, use chsh -s /bin/bash.
Then echo $SHELL to verify if the shell has changed.
Terminal -> Preference -> profile -> Shell -> Run command : source ~/.bash_profile
Tick on run inside shell.
After doing all those , just logout and check weather everything works fine or not
I tried the approved answer. Changing the .zshrc file works for one of my machines. But for the other one, when I run ps -p $$, it is -sh under the command. And I changed both bash and zsh files, neither of them works for me this time.
So I found this
https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html
it mentioned
"When Bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. "
so I went to that file /etc/profile and add "source ~/.bashrc" in that file. Then it works since every time a terminal is opened, it runs the command in that /etc/profile file.
Not sure if this is the best solution but it works.
sudo nano /etc/bashrc and change that, restarted the terminal and it finally remembered with command. Tried ~/.bash_profile and ~/.bashrc without success, just wasn't sourcing it.
Go to “Preferences/Profiles then look in the right window and find “shell”.
Once in that if your “Startup Run Command” hasn’t been turned on. Click the box to turn it on and in the command section type:
(If you made a .zsh file)
source .zsh ; clear
(If you made a .bash_profile)
source .bash_profile ; clear
Doing this ; clear
Will clear your terminal to a new page so that you don’t see your terminal display:
“Last login: etc
User#user-Mac ~ % source .zsh
If you typed the commands as I said you should just get this:
User#user-Mac ~ %
That way you will be greeted with a clear page with no extra jumbo. Also to make sure that your .zsh or .bash_profile aliases work type the following command to see a list of your custom aliases:
Alias
One alias I like to do is
alias LL=“ls -la”
This will display a tree or the directory you are in as well as hidden files.

MacVim doesn't load $PATH env variable from .bash_profile [duplicate]

I am using MacVim (basically gvim for the mac).
If I open macvim from the command line then my $PATH variable will be properly set.
If I open macvim via point and click with the finder, the $PATH variable will NOT be properly set.
Can anyone give me some insight?
Note: I know at least part of my path is set in ~/.bashrc, but I am not sure where the rest of it is set.
Examples:
If I open macvim from the terminal:
% gvim basic.tex
And then in MacVim I go:
:!echo $PATH
/opt/local/bin:/opt/local/sbin:/sw/bin:/sw/sbin:/Applications/MacVim.app/Contents/M
acOS:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/texbin:/usr/X11R6/bin
This is the right path.
When I open the file with the mouse (in finder)
When I go:
:!echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin
It gives me a little path. Why?
I had this same issue but it only appeared after setting my default shell to zsh like so
export SHELL=/bin/zsh
It seems that there is a bug in the OS X zsh setup. The work around in brief is to merge /etc/zshenv into /etc/zprofile. In my case I didn't have a /etc/zprofile so just moving over the file did the trick:
sudo mv /etc/zshenv /etc/zprofile
This post describes the solution in more detail.
For me, simply creating a new symbolic link from .zprofile to .zshrc did the trick:
ln -s ~/.zshrc ~/.zprofile
The place to set environment variables
on the Mac for GUI applications (those started via loginwindow, the
Finder, etc.) is ~/.MacOSX/environment.plist
Alternately in MacVim you can choose to launch vim processes in a login-shell (look in the preferences).
For more info see this post.
The difference in the PATHs probably has something to do with the difference between a login shell (logging in) and a non-login shell (bringing up a console).
From the bash man page:
When bash is invoked as an interactive login shell ... it looks for ~/.bash_profile...
When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc...
What I did to get around this issue was to add the following code to my ~/.bash_profile, telling it to source my ~/.bashrc if it exists:
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

$PATH variable not properly set in gvim/MacVim when it is opened from the finder

I am using MacVim (basically gvim for the mac).
If I open macvim from the command line then my $PATH variable will be properly set.
If I open macvim via point and click with the finder, the $PATH variable will NOT be properly set.
Can anyone give me some insight?
Note: I know at least part of my path is set in ~/.bashrc, but I am not sure where the rest of it is set.
Examples:
If I open macvim from the terminal:
% gvim basic.tex
And then in MacVim I go:
:!echo $PATH
/opt/local/bin:/opt/local/sbin:/sw/bin:/sw/sbin:/Applications/MacVim.app/Contents/M
acOS:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/texbin:/usr/X11R6/bin
This is the right path.
When I open the file with the mouse (in finder)
When I go:
:!echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin
It gives me a little path. Why?
I had this same issue but it only appeared after setting my default shell to zsh like so
export SHELL=/bin/zsh
It seems that there is a bug in the OS X zsh setup. The work around in brief is to merge /etc/zshenv into /etc/zprofile. In my case I didn't have a /etc/zprofile so just moving over the file did the trick:
sudo mv /etc/zshenv /etc/zprofile
This post describes the solution in more detail.
For me, simply creating a new symbolic link from .zprofile to .zshrc did the trick:
ln -s ~/.zshrc ~/.zprofile
The place to set environment variables
on the Mac for GUI applications (those started via loginwindow, the
Finder, etc.) is ~/.MacOSX/environment.plist
Alternately in MacVim you can choose to launch vim processes in a login-shell (look in the preferences).
For more info see this post.
The difference in the PATHs probably has something to do with the difference between a login shell (logging in) and a non-login shell (bringing up a console).
From the bash man page:
When bash is invoked as an interactive login shell ... it looks for ~/.bash_profile...
When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc...
What I did to get around this issue was to add the following code to my ~/.bash_profile, telling it to source my ~/.bashrc if it exists:
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

Resources