Aliases in .bash_profile not working properly [closed] - macos

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have been trying to alter the .bash_profile that is in my root directory, but have been running into some problems. I am on OS X, Yosemite, on a Macbook Pro. As I understand it, the .bash_profile file contains the script that is called automatically whenever the Terminal app is opened and the bash shell starts.
This is what I currently have written in that file:
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
This works perfectly fine. However, I want to add an alias (right underneath the above two lines) as follows:
alias test='cd ..'
However, when I save this and start up the Terminal, I get the following message:
-bash: alias: ..": not found
Replacing the single quotes with double quotes doesn't help, nor does taking them away altogether. Curiously however, the following alias works:
alias c=clear
When I type c into the terminal, it clear the screen, as you would expect. However, if I instead entered this line with quotes in the bash profile as:
alias c='clear'
Then I will get the following whenever I enter c into the Terminal:
-bash: 'clear': command not found
Note that I do not get an error message on startup for this alias.
What am I doing wrong? Is there a setting I need to change somewhere to get aliases to work properly? I have seen previous examples of aliases and they simply do not work for me.

It looks like shell is not accessing your .bash_profile when logging via terminal.
.bash_profile is a config file of bash shell. When bash shell starting, shell will execute the commands in .bash_profile. But there are many kinds of shells, and different shells execute different config file.
Terminal is a software to receive user input, shell will execute commands.You can use cat /etc/shells to list acceptable shells. For example:
$ cat /etc/shells
/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
The default shell is bash shell on Mac OX. But if you have installed zsh, the default shell is zsh, when zsh shell starting, shell will find out the file named .zshrc, and execute the commands in .zshrc.
You can use echo $SHELL to determine the current shell:
$ echo $SHELL
/bin/bash
-> echo $SHELL
/bin/zsh
If your default shell is zsh, .bash_profile don't work. The config file of zsh is .zshrc. And I guess your problem is that your default shell is not bash shell. For example, if your default shell is zsh, you should config the .zshrc , just add
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
or other config to ~/.zshrc.
Then source ~/.zshrc, and the config will work immediately.

Wrapping the command with double quotation worked for me. I was trying with every possible way mentioned in this thread and none of them worked. Then I replaced single quote to double and that worked.
alias mysql_start="sudo /path/to/server/mysql.server start"

For me, it didn't work in the same terminal. I had to open a new one to get it work.

The killer for me was space and single quotes.
alias test="cd .." worked.
Don't put any spaces between alias_name=

User level 'system' files need to contain 'plain text'. How to set/configure this for your 'editor-of-choice' can vary (I don't use a Mac so I'm not much help with that.) Soo,
create your profile 'from the shell' by appending lines directly, i.e. remove the 'bad lines and then:
echo "some command string" >> ~/path/bash_profile
use 'vi' or 'vim' ## should be 'safe'
review your file via:
cat -v ~/path/bash_profle | more ## see any 'funny chars'? or
cat -ve ~/path/bash_profle | more ## see any 'funny chars'? or
or
vi ~/path/bash_profle # then set 'control codes' to 'on'
set list ## see any 'funny chars' for your 'quotes'?

What I realised is that Mac has option for smart quotes and dashes.
alias ..='cd ../' is different from alias ..=‘cd ../‘, where the former works but the latter doesn't.

You can run alias custom auto startup in ~/.bash_profile or ~/.alias_file... by paste file name alias in ~/.bashrc if you use bash or ~/.zshrc if you use zsh.
Ex:
if [ -s ~/.bash_profile ]; then
source ~/.bash_profile;
fi

I fixed this by editing my .bash_profile in vim or nano something about text edited messed it up unsure why.
I tried to edit in in notepad just using "open .bash_profile" however something about the symbols translated wrong. So you have to either "vim .bash_profile" or "nano .bash_profile"
Doing this corrected this problem for me.

Make sure that the alias commands are together in the .bash_profile too. That is what was wrong with mine.
Also, if you just type in alias in the terminal, it should list all the known alias commands, so if you don't see your command you know something is wrong.

If alias is not recognizing, first identify what shell you're using when you open up your terminal or commandlineprompt echo $SHELL. For me it's /bin/bash so i'll input my aliases in ~/.bash_profile
Take note, the file ~/.bash_profile can store both aliases to call out during a terminal session and autorun those same aliases upon opening up a terminal session. The example below would be to open up a file to edit by text via vim-software. You may change it other text-editors as you wish. You can see the difference by closure-method.
ALIAS CALLOUT (uses parenthesis)
alias editbp="vim ~/.bash_profile"
ALIAS AUTORUN (uses tilda)
alias editbp=`vim ~/.bash_profile`

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.

~/.profile, ~/.bashrc, and ~/.bash_profile not running on new terminal start up

I am trying to create a permanent alias for my terminal. I put the alias in my ~/.profile, ~/.bashrc, and ~/.bash_profile files, previously empty. When I start a new terminal, bash does not recognize the alias, but if I source any of them, it does. Why are these not getting run when I open a terminal? I am on OSX.
Newer MacOS versions use zsh as the default shell for both Terminal and iTerm2. Run echo $SHELL to confirm if this is the case for you.
Zsh looks for a .zshrc file upon shell startup, so to continue using zsh while sourcing the contents of your bash profile, you can run the following:
echo "source ~/.bash_profile" >> ~/.zshrc
Open a new terminal window for the changes to take effect.
Two things need to happen here when using iTerm to get the loading of dotfiles to work.
First you should add the following to your .bash_profile
[[ -s ~/.bashrc ]] && source ~/.bashrc
Secondly you will need to ensure that in iTerm preferences your terminal is set to launch a login shell.
Hope this helps!
Using the default mac terminal, what worked for me was to add a command to run on start up to source my .bash_profile.
Preferences > Profile > Startup > Add command 'source ~/.bash_profile'
Mac terminal preferences window screenshot
Might be considered to be a bit hacky, but it does the trick.
Adding source ~/.profile to my .bash_profile worked for me.
As of High Sierra, both Terminal and iTerm want to load ~/.profile first. So I suggest you put one line in your .profile to make your Mac work like other Unixes:
source ~/.bash_profile
By editing this one file, you won't have to search through the menus of multiple apps to override Apple's bizarre behavior in each.
As of Catalina the default shell is now zsh. You can change it back to bash with chsh -s /bin/bash and that should load your .profile or .bash_profile
Why are your shell's initialization files not loading?
As with most things, It Depends ™
I recently experienced the same phenomenon and went through the following exercise to resolve it:
I use iTerm. iTerm runs a login shell by default. Verify in iTerm Preferences > General > Command > (*) Login Shell
Therefore, I know that ~/.bash_profile will always be called.
Knowing that, I put the following in my ~/.bash_profile file:
for file in ~/.{bashrc,bash_exports,bash_aliases,bash_functions}; do
[ -r "$file" ] && source "$file"
done
unset file
Notice that I use separate files for .bashrc, .bash_exports, etc. It keeps things separate and simple.
Note also that /etc/profile is loaded first, but since I have never used that system wide init file, I knew that that was not my problem. For details check out $ man bash
So, I started with my ~/.bash_profile file.
I found that when I installed Canopy Express that it's installer replaced the contents of my ~/.bash_profile file with the following content:
# Added by Canopy installer on 2017-04-19
# VIRTUAL_ENV_DISABLE_PROMPT can be set to '' to make the bash prompt show that Canopy is active, otherwise 1
alias activate_canopy="source '/Users/lex/dev/python/User/bin/activate'"
# VIRTUAL_ENV_DISABLE_PROMPT=1 source '/Users/lex/dev/python/User/bin/activate'
p.s. Canopy is an excellent, free python IDE, that I highly recommend.
Fortunately, I backup my ~/.bash* files so restoring that was easy and quickly fixed my issue.
My advice would be to understand the order of calls to your initialization files and start with the first one and work your way through them until you find the problem.
Oh, and you may want to verify which shell you are using (I use bash):
~ $ echo $SHELL
/usr/local/bin/bash
I am guessing you may use another shell, such as bash, tcsh, sh, zsh etc.
Put source .bash_profile into your appropriate 'bashrc' file will make the auto loading recovered, i.e.
.login for tcsh, .bash_profile for bash, .zshrc for zsh
My issue was solved by unchecking Preferences > General > tmux >
Use "tmux" profile rather than profile of the connecting session
Most likely, you need to create the files yourself as they appear not to exist by default. You should give them execute permission to make them run.
~ % sudo chmod 700 ~/.bash_profile
Also, you should check the ownership of the files. They should belong to current user rather than root. Otherwise, you will get permission denied error.
~ % ls -a -l
~ % sudo chown <user_name> ~/.bash_profile
Finally, please note that bash looks in your home directory for .bash_profile, .bash_login, and .profile in order. Bash will stop looking if the first is found.
This means if you have both .bash_profile and .profile files, the .profile will not run.
For more information
Hope this would help you.
Little late to the party but it seems that the file .zprofile is the equivalent to that of .bash_profile when loading zsh. I used this instead to execute a few commands on startup. Of course this only valid for a specific iTerm setup with zsh.
https://zsh.sourceforge.io/Intro/intro_3.html

How to load ~/.bash_profile when entering bash from within zsh?

I've used bash for two years, and just tried to switch to zsh shell on my OS X via homebrew. And I set my default (login) shell to zsh, and I confirmed it's set properly by seeing that when I launch my Terminal, it's zsh shell that is used in default.
However, when I try to enter bash shell from within zsh, it looks like not loading ~/.bash_profile, since I cannot run my command using aliases, which is defined in my ~/.bash_profile like alias julia="~/juila/julia", etc.. Also, the prompt is not what I set in the file and instead return bash-3.2$.
For some reasons, when I set my login shell to bash, and enter zsh from within bash, then ~/.zshrc is loaded properly.
So why is it not loaded whenever I run bash from within zsh? My ~/.bash_profile is symbolic linked to ~/Dropbox/.bash_profile in order to sync it with my other computers. Maybe does it cause the issue?
Open ~/.zshrc, and at the very bottom of the file, add the following:
if [ -f ~/.bash_profile ]; then
. ~/.bash_profile;
fi
Every time you open the terminal, it will load whatever is defined in ~/.bash_profile (if the file exist). With that, you can keep your custom settings for zsh (colors, and etc). And you get to keep your custom shell settings in .bash_profile file.
This is much cleaner than using bash -l IMO.
If you prefer putting your settings in .bashrc, or .bash_login, or .profile , you can do the same for them.
Similarly, you could also move the common profile settings to separate file, i.e. .my_common_profile, and add the following to both .bash_profile and .zshrc:
if [ -f ~/.my_common_profile ]; then
. ~/.my_common_profile;
fi
An interactive bash reads your ~/.bash_profile if it's a login shell, or your ~/.bashrc if it's not a login shell.
A typical .bash_profile will contain something like:
if [ -f ~/.bashrc ]; then . ~/.bashrc; fi
so .bashrc can contain commands to be executed by either login or non-login shells.
If you run bash -l rather than just bash, it should read your .bash_profile.
Reference: https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html
For those who have just installed zsh and want their alias from bash to work on zsh do the following
Open .zshrc file in vim like so
vi ~/.zshrc
Scroll to the bottom
click "i" to enable write mode
Tell zsh to load items from bash_profile when needed like so
source ~/.bash_profile
Write and quit like so
:wq
Refresh your zsh like so
source ~/.zshrc
That's it. Now all your saved alias in .bash_profile will be ready to use in zsh.
To complement #Keith Thompson's excellent answer:
macOS:
As #chepner puts it succinctly (emphasis mine):
In OS X, bash is not used as part of the initial [at boot time] login process, and the Terminal.app (or other terminal emulators) process exists outside any pre-existing bash sessions, so each new window [or tab - read: interactive bash shell] (by default) treats itself as a new login session.
As a result, some OSX users only ever create ~/.bash_profile, and never bother with ~/.bashrc, because ALL interactive bash shells are login shells.
Linux:
On Linux, the situation is typically reversed:
bash shells created interactively are [interactive] NON-login shells, so it is ~/.bashrc that matters.
As a result, many Linux users only ever deal with ~/.bashrc.
To maintain bash profiles that work on BOTH platforms, use the technique #Keith Thompson mentions:
Put your definitions (aliases, functions, ...) in ~/.bashrc
Add the following line to ~/.bash_profile
[[ -f ~/.bashrc ]] && . ~/.bashrc
Copy the contents from ~/.bash_profile and paste them at the bottom of ~/.zshrc file.
For ZSH users on MacOs, I ended up with a one liner.
At the very bottom of the ~/.zshrc I added the following line :
bash -l
What it does is simply load the .bash_profile settings(aliases, function, export $PATH, ...)
If you decide to get rid of ZSH and go back to plain BASH, you'll be back to normal with no hassle at all.
If this is something that you do infrequently, or it just isn't appropriate to make changes, you can also 'source' the .bash_profile after launching the child bash shell.
. ~/.bash_profile
This will pull in the settings you make in the .bash_profile script for the life of that shell session. In most cases, you should be able to repeat that command, so it's also an easy way to test any changes that you make without needing to do a full login, as well as bring all of your existing shell sessions up-to-date if you make upgrades to the .bash_profile &/or .bashrc files.
For macOS Big Sur (Version 11.5.2)
Open .zshrc
For example: sudo nano ~/.zshrc
At the end of the file add source ~/.bash_profile
Every time you open the terminal the contents inside the bash profile will be loaded.
Recently I installed oh-my-zsh on OS X and set zsh as default shell and faced the same problem.
I solved this problem by adding source ~/.bash_profile at the end of .zshrc file.
I am using a zsh framework called oh my zsh and I have tried most of the solutions listed here and it broke the format for my custom theme. However, these steps worked for me.
Add new alias(es) at the bottom of my .bash_profile
vi ~/.bash_profile
Make zsh to load items from .bash_profile
source ~/.bash_profile
Refresh zsh
source ~/.zshrc
Restart OSX Terminal app
Try your new alias!
If you'd like to be "profile-centric", you can create .profile as a single source of truth, then load it from both .bash_profile and .zprofile.
.profile
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
# etc., etc.
.bash_profile and .zprofile
if [ -f ~/.profile ]; then
. ~/.profile;
fi
I found this helped bash scripts find the right PATH, etc., and helped me keep configuration in one place.

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.

How to reload .bash_profile from the command line

How can I reload file .bash_profile from the command line?
I can get the shell to recognize changes to .bash_profile by exiting and logging back in, but I would like to be able to do it on demand.
Simply type source ~/.bash_profile.
Alternatively, if you like saving keystrokes, you can type . ~/.bash_profile.
. ~/.bash_profile
Just make sure you don't have any dependencies on the current state in there.
Simply type:
. ~/.bash_profile
However, if you want to source it to run automatically when terminal starts instead of running it every time you open terminal, you might add . ~/.bash_profile to ~/.bashrc file.
Note:
When you open a terminal, the terminal starts bash in (non-login) interactive mode, which means it will source ~/.bashrc.
~/.bash_profile is only sourced by bash when started in interactive login mode. That is typically only when you login at the console (Ctrl+Alt+F1..F6), or connecting via ssh.
If you don't mind losing the history of your current shell terminal, you could also do
bash -l
That would fork your shell and open up another child process of bash. The -l parameter tells Bash to run as a login shell. This is required, because .bash_profile will not run as a non-login shell. For more information about this, read here.
If you want to completely replace the current shell, you can also do:
exec bash -l
The above will not fork your current shell, but replace it completely, so when you type exit it will completely terminate, rather than dropping you to the previous shell.
You can also use this command to reload the ~/.bash_profile for that user. Make sure to use the dash.
su - username
I like the fact that after you have just edited the file, all you need to do is type:
. !$
This sources the file you had just edited in history. See What is bang dollar in bash.
You just need to type . ~/.bash_profile.
Refer to What does 'source' do?.
Save the .bash_profile file
Go to the user's home directory by typing cd
Reload the profile with . .bash_profile
Add alias bashs="source ~/.bash_profile" into your Bash file.
So you can call bashs the next time.
If the .bash_profile file does not exist, you can try to run the following command:
. ~/.bashrc
or
source ~/.bashrc
instead of .bash_profile.
You can find more information about bashrc.
Use
alias reload!=". ~/.bash_profile"
Or if want to add logs via functions:
function reload! () {
echo "Reloading bash profile...!"
source ~/.bash_profile
echo "Reloaded!!!"
}
While using source ~/.bash_profile or the previous answers works, one thing to mention is that this only reloads your Bash profile in the current tab or session you are viewing. If you wish to reload your bash profile on every tab/shell, you need to enter this command manually in each of them.
If you use iTerm, you can use CMD⌘ + Shift + I to enter a command into all current tabs. For terminal it may be useful to reference this issue;
I use Debian and I can simply type exec bash to achieve this. I can't say if it will work on all other distributions.
I am running macOS v10.12 (Sierra) and was working on this for a while (trying all recommended solutions). I became confounded, so I eventually tried restarting my computer! It worked.
My conclusion is that sometimes a hard reset is necessary.
Simply re-sourcing the file won't "reload" in the sense that something is first unloaded, then loaded again. If that is what you want you can do:
hash -r && _SHOW_MESSAGES=1 exec -a -bash bash

Resources