Issue with aliases in Terminal shell - bash

I'm set /bin/bash as my default shell in Terminal (macos) and my aliases, written in '~/.bashrc' don't work (command not found).
But if I write in Terminal /bin/bash command, I'll switch to new bash3.2 shell, and then I can use it.
Is there some another '.bashrc' where I should write down my aliases?
P.S. I used Terminal preferences to made /bin/bash default
and chsh -s /bin/bash/ command too.

Terminal starts a login shell rather than an "ordinary" interactive shell, because Terminal itself is not run from a shell that inherited an environment from a login shell.
Ordinary interactive shells source .bashrc; login shells source the first of .profile, .bash_login, or .bash_profile it finds. I recommend sourcing .bashrc directly from .bash_profile.
Your aliases work when you run /bin/bash directly because that does not start another login shell, and so .bashrc is sourced as expected.

As above, if you add the following line to .profile in your home directory:
source ~/.bashrc
Once you save it and create a new Terminal session, it will automatically source your .bashrc with your alias settings.

You want /bin/bash -l to run as if you used a login shell. See the INVOCATION section of man bash for more information.

Related

Terminal opened in vim doesn't have .bash_profile sourced implicitly

I am running vim 8.2 and when I execute the command "term" inside vim, it opens up a terminal in the same window but it doesn't have the .bash_profile sourced implicitly. I need to do that before using the terminal.
Is there a way to fix this?
According to the bash man page:
.bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.
When an interactive shell that is not a login shell is started, Bash reads and executes commands from ~/.bashrc, if that file exists.
Since you are using, non-login shell, I would suggest you to put all your aliases, shell options in the bashrc.

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.

"source" command on mac

I have a .bashrc file on Mac OS in which I set some aliases. After I make it and I type source .bashrc, the aliases work perfectly. However if open another shell, my shortcut command will not be recognized and I need to do source .bashrc again. How can it make it once and for all?
Terminal and iTerm 2 open new shells as login shells by default. When Bash is opened as a login shell, it reads ~/.bash_profile but not ~/.bashrc.
See https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html:
Invoked as an interactive login shell, or with --login
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 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.
So either:
Use ~/.bash_profile instead of ~/.bashrc.
Use ~/.bashrc but source ~/.bashrc from ~/.bash_profile.
Tell your terminal application to open new shells as non-login shells.
I have done the last two. For example tmux and the shell mode in Emacs open new shells as non-login shells. I still source ~/.bashrc from ~/.bash_profile because Bash is opened as a login shell when I ssh to my computer.
If you are on Mac and you want to source ~/.bash_profile automatically on when a terminal is opened
Open Terminal
Go to Preferences
Go to Profiles
Select then open shell
Add this command source ~/.bash_profile in Run command
If you want to source your bash_profile file everytime you open a new tab you can also set up a command on Iterm.
Go to Preferences -> Profiles -> General -> Command
I encounter the same problem and I solve it.
The macos have shift the default shell from bash to zsh.
So I try to modify the ~/.bashrc and ~/.bash_profile and source that but just work to current Terminal.
The fact is you should modify the ~/.zshrc profile file.
Try it my friend!
nano ~.bash_profile (Opens the ~.bash_profile)
aliasname(){
ssh user#ipaddress
}(You can add any command for the alias, I have shown the ssh command for a particular IP address)
Press Control+O and press Enter (Save the file). Control+X(Exit nano editor)
source .bash_profile
Now you have a persistent command across all the terminals whenever you type aliasname.

Why do I have to keep using `source ~/.profile` to get settings in place?

I have a couple of bash scripts that I want to make sure runs by default and I'm currently storing them in ~/.profile on my mac. Is that the wrong place to be storing them? I've heard of others and tried them (like ~/.bashrc, ~/.bash_profile, etc), but they don't seem to be working.
What is the difference between all of these and which one do I put the scripts in so that it configures on runtime and I don't have to call $ source ~/.profile every time I open the terminal?
If both ~/.bash_profile and ~/.profile exist, bash only reads ~/.bash_profile when it is invoked as an interactive login shell.
https://www.gnu.org/s/bash/manual/html_node/Bash-Startup-Files.html:
Invoked as an interactive login shell, or with --login
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 executes commands from the first one that exists and is readable.
[...]
Invoked as an interactive non-login shell
When an interactive shell that is not a login shell is started, Bash reads and executes commands from ~/.bashrc, if that file exists.
~/.profile is also used by other shells.
Terminal and iTerm open new shells as login shells by default (by executing something like login -pf $USER), but many GNU/Linux terminal applications open new shells as non-login shells. OS X users often use ~/.bash_profile instead of ~/.bashrc.
+-----------------+
| |
interactive shell -->| ~/.bashrc |
| |
+-----------------+
interactive shell will source ~/.bashrc automatically.
Take a look at Should the .bashrc in the home directory load automatically?
I did these to rectify the problem:
cat .bash_profile >> .profile
rm .bash_profile
the alternative is:
echo "source ~/.profile" >> .bash_profile
Make sure if you do source ~/.profile in your .bashrc that you comment out or remove any commands (in .profile) to call or source .bashrc in your .profile or it will loop forever and you will never get a prompt.
Different setups of bash will automatically source different files depending on their configuration. The nearly universal file that is always sourced is ~/.bashrc - this is a bash core thing that it will load this file. In that file, you should add your line to source ~/.profile and you'll be good to go!
-Edit-
From my Linux and my colleague's Mac:
$ echo "echo hello" >> ~/.profile
$ echo "source ~/.profile" >> ~/.bashrc
$ bash
Hello
$

Should aliases go in .bashrc or .bash_profile? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What's the difference between .bashrc, .bash_profile, and .environment?
It seems that if I use
alias ls='ls -F'
inside of .bashrc on Mac OS X, then the newly created shell will not have that alias. I need to type bash again and that alias will be in effect.
And if I log into Linux on the hosting company, the .bashrc file has a comment line that says:
For non-login shell
and the .bash_profile file has a comment that says
for login shell
So where should aliases be written in? How come we separate the login shell and non-login shell?
Some webpage say use .bash_aliases, but it doesn't work on Mac OS X, it seems.
The reason you separate the login and non-login shell is because the .bashrc file is reloaded every time you start a new copy of Bash. The .profile file is loaded only when you either log in or use the appropriate flag to tell Bash to act as a login shell.
Personally,
I put my PATH setup into a .profile file (because I sometimes use other shells);
I put my Bash aliases and functions into my .bashrc file;
I put this
#!/bin/bash
#
# CRM .bash_profile Time-stamp: "2008-12-07 19:42"
#
# echo "Loading ${HOME}/.bash_profile"
source ~/.profile # get my PATH setup
source ~/.bashrc # get my Bash aliases
in my .bash_profile file.
Oh, and the reason you need to type bash again to get the new alias is that Bash loads your .bashrc file when it starts but it doesn't reload it unless you tell it to. You can reload the .bashrc file (and not need a second shell) by typing
source ~/.bashrc
which loads the .bashrc file as if you had typed the commands directly to Bash.
Check out http://mywiki.wooledge.org/DotFiles for an excellent resource on the topic aside from man bash.
Summary:
You only log in once, and that's when ~/.bash_profile or ~/.profile is read and executed. Since everything you run from your login shell inherits the login shell's environment, you should put all your environment variables in there. Like LESS, PATH, MANPATH, LC_*, ... For an example, see: My .profile
Once you log in, you can run several more shells. Imagine logging in, running X, and in X starting a few terminals with bash shells. That means your login shell started X, which inherited your login shell's environment variables, which started your terminals, which started your non-login bash shells. Your environment variables were passed along in the whole chain, so your non-login shells don't need to load them anymore. Non-login shells only execute ~/.bashrc, not /.profile or ~/.bash_profile, for this exact reason, so in there define everything that only applies to bash. That's functions, aliases, bash-only variables like HISTSIZE (this is not an environment variable, don't export it!), shell options with set and shopt, etc. For an example, see: My .bashrc
Now, as part of UNIX peculiarity, a login-shell does NOT execute ~/.bashrc but only ~/.profile or ~/.bash_profile, so you should source that one manually from the latter. You'll see me do that in my ~/.profile too: source ~/.bashrc.
From the bash manpage:
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 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.
When a login shell exits, bash
reads and executes commands from the
file ~/.bash_logout, if it exists.
When an interactive shell that is not a login shell is started, bash
reads and executes commands from ~/.bashrc, if that file exists. This
may be inhibited by using the --norc option. The --rcfile file option
will force bash to read and execute commands from file instead of
~/.bashrc.
Thus, if you want to get the same behavior for both login shells and interactive non-login shells, you should put all of your commands in either .bashrc or .bash_profile, and then have the other file source the first one.
.bash_profile is loaded for a "login shell". I am not sure what that would be on OS X, but on Linux that is either X11 or a virtual terminal.
.bashrc is loaded every time you run Bash. That is where you should put stuff you want loaded whenever you open a new Terminal.app window.
I personally put everything in .bashrc so that I don't have to restart the application for changes to take effect.

Resources