Bash startup script in Windows - windows

I am using the Bash shell with Git in Windows. I have a number of aliases I wish to setup when I open the shell. Is there a configuration file that is invoked for each Bash instance that is brought up?

Thanks #Blender, that was the answer. Here is what I did...
Start bash (Git bash)
cd
touch .bashrc
notepad .bashrc
source .bashrc # To make changes available without restart
entered aliases...save
close bash
start bash
aliases are now available!

Related

Issue with aliases in Terminal shell

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.

How to set `pipenv shell` prompt to match the Terminal's PS1 prompt?

I'm using MacOS and bash.
I'm using pipenv for virtual environments and I really want to make the look of my virtual shell match the look of my standard shell.
When I'm outside of pipenv, I set my .bash_profile so I could have the time, a green command prompt that shows the full path of my working directory and, if that directory has a git repo, it shows my current branch at the end.
I've searched for the correct file to edit to get the same look for each new pipenv shell, but I'm not having any luck finding the right file to copy my profile preferences to.
I'm wondering how I can set it up with my standard PS1 terminal appearance (colors and current git branch, etc).
pipenv-FAQ
Look at the shell does not show the virtualenv’s name in prompt:
This is intentional. You can do it yourself with either shell plugins, or clever PS1 configuration. If you really want it back, use
And for the "shell plugins"
You need to look not for "pipenv+bash" but for virtualenv+bash prompt solutions, such as:
How do I change the default virtualenv prompt?
It's hard for me to recommend "Which way is the best?"
(I'm using zsh myself)
I've searched for the correct file to edit to get the same look for each new pipenv shell, but I'm not having any luck finding the right file to copy my profile preferences to.
For bash, you can create and modify ~/.bashrc.
The pipenv shell command is a wrapper for virtualenv, which checks for a ~/.bashrc file when it activates a virtual environment for a bash shell. Anything in the ~/.bashrc file gets used in the spawned shell, including prompts, aliases, and other things you customized in your shell.
$ cat ~/.bashrc
cat: /Users/gino/.bashrc: No such file or directory
$ pipenv shell
Launching subshell in virtual environment…
bash-5.0$ . /Users/gino/.venvs/test-oiEjhH94/bin/activate
(test) bash-5.0$ ll
bash: ll: command not found
(test) bash-5.0$ exit
$ vim ~/.bashrc
$ cat ~/.bashrc
PS1='\[\e[1;33m\]\u#\W\$\[\e[0m\] '
alias ll="ls -Flh"
$ pipenv shell
Launching subshell in virtual environment…
gino#test$ . /Users/gino.mempin/.venvs/test-oiEjhH94/bin/activate
(test) gino#test$
(test) gino#test$ type ll
ll is aliased to `ls -Flh'
What you can do is to have a ~/.bash_prompt file where you set all your prompt configs (PS1) and a ~/.bash_aliases file where you set all your aliases. Then, source them both in the ~/.bash_profile (for the standard shell) and in the ~/.bashrc (for the pipenv shell).
~/.bash_prompt
# Format the prompt
# Shows up as:
# machine-name#current-working-directory$
PS1='\[\e[1;33m\]\u#\W\$\[\e[0m\] '
# other prompt configs
~/.bash_aliases
alias ll="ls -FlhpG"
# other aliases
~/.bash_profile
# For "main" shell (i.e. Terminal, VS Code terminal)
. ~/.bash_prompt
. ~/.bash_aliases
~/.bashrc
# For `pipenv shell`
. ~/.bash_prompt
. ~/.bash_aliases
A better solution would be to tell pipenv shell or virtualenv to reuse the configs in the ~/.bash_profile, but I haven't got that successfully to work (Terminal, pipenv, VS Code).
macOS 10.15
bash 5.0 (installed via Homebrew)
pipenv 2020.8.13

Bash alias when running Perlbrew

I am using Perlbrew in a brand new server (CentOs 6).
In my previous system (RedHat 4) WiTHOUT Perlbrew I used to use some aliases in the bash shell, like alias nreload='/usr/local/nginx/sbin/nginx -s reload' for example, to restart Nginx just putting the line in the .bash_profile file.
Now, with Perlbrew, I tried it in .bash_profile and in .bashrc, with no luck. I can make aliases on the fly, but I can't have them permanently available.
I followed the Perlbrew installation tips and copied the line source ~/perl5/perlbrew/etc/bashrc in the .bashrc file. I thought it should activate the Perlbrew Perl just by logging-in to the server, but it doesn't. So I must write source ~/perl5/perlbrew/etc/bashrc; everytime I need to use the Perlbrew Perl. It doesn't seems to be the right way.
Maybe the real problem is how I run Perlbrew. Maybe I do not understand the concept of Perlbrew opening another shell. If there is another shell, a subshell, how can I use alias in it?
Here is the content of my .bash_profile:
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
source ~/perl5/perlbrew/etc/bashrc
export PATH
alias nreload='/usr/local/nginx/sbin/nginx -s reload'
BTW: the server is a remote one, and I control it from a local Macintosh, using Terminal. I have read something about Terminal is more a visual tool than a real Unix terminal, but I do not completely understand it.
I have found the way: aliases must be in .bashrc
I connect using (Macintosh) Terminal, and when logged to the online server I just "activate" a new shell typing "/bin/bash".
In this new shell I have the right Perlbrew Perl, an all the alias, because this new shell takes everything from the .bashrc file.
What confused me was that "a new shell" is not really "a new terminal window" as I was expecting, but a new "process", and this new process, obviously uses .bashrc as a start point.

"Git Bash here" is not preserving bash history between sessions [duplicate]

This question already has answers here:
.bash_history does not update in Git for Windows (git bash)
(11 answers)
Closed 2 years ago.
I am running Git-1.8.0-preview20121022 on Windows 7 and the install was with "Git Bash Only" (least intrusive to Windows cmd).
When I open the Git Bash from the start menu shortcut, everything is fine with the history.
But when the Git Bash here context menu (either the git-cheetah shell extension one or the simpler registry one) is what launched a session, the commands from that session are not saved to the .bash_history.
How could figure out why this is happening? Or better yet, does someone know how to fix this?
You should be able to fix this by adding this line to your ~/.bash_profile
PROMPT_COMMAND='history -a'
As mentioned here: https://stackoverflow.com/a/60718848/6680510
Create the following files
~/.bash_profile
~/.bashrc
And put the following line in both of them
PROMPT_COMMAND='history -a'
To do this from the console (git bash) itself use the following
commands
echo "PROMPT_COMMAND='history -a'" >> ~/.bash_profile
echo "PROMPT_COMMAND='history -a'" >> ~/.bashrc
What history -a means
From history --help command
-a append history lines from this session to the history file
What is PROMPT_COMMAND ?
Bash provides an environment variable called PROMPT_COMMAND. The contents of this variable are executed as a regular Bash command just
before Bash displays a prompt.
Difference between .bash_profile AND .bashrc
.bash_profile is executed for login shells, while .bashrc is
executed for interactive non-login shells.
When you login (type username and password) via console, either
sitting at the machine, or remotely via ssh: .bash_profile is executed
to configure your shell before the initial command prompt.
But, if you’ve already logged into your machine and open a new
terminal window (xterm) then .bashrc is executed before the window
command prompt. .bashrc is also run when you start a new bash instance
by typing /bin/bash in a terminal.
On OS X, Terminal by default runs a login shell every time, so this is
a little different to most other systems, but you can configure that
in the preferences.
References
https://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x264.html
https://apple.stackexchange.com/questions/51036/what-is-the-difference-between-bash-profile-and-bashrc
Putting
PROMPT_COMMAND='history -a ~/.bash_history'
into the .bash_profile did it for me.

.bash_history does not update in Git for Windows (git bash)

I am using Git for Windows (ver. 1.7.8-preview20111206) and even though I have a .bash_history file in my HOME folder, it never automatically gets updated. When I start Git Bash, I can see in the history commands that I manually added to the .bash_history file, but it does not get updated automatically.
I used the shopt -s histappend command to make sure that the history gets saved every time I close the shell, but it does not work.
If I manually use the history -w command, then my file gets updated, but I would want to understand why the shopt command does not work as I understand it should.
Anyone can tell me why is this behavior happening?
I put this in my ~/.bash_profile
PROMPT_COMMAND='history -a'
Create the following files
~/.bash_profile
~/.bashrc
And put the following line in both of them
PROMPT_COMMAND='history -a'
To do this from the console (Git Bash) itself, use the following commands:
echo "PROMPT_COMMAND='history -a'" >> ~/.bash_profile
echo "PROMPT_COMMAND='history -a'" >> ~/.bashrc
What history -a means
From the history --help command
-a append history lines from this session to the history file
What is PROMPT_COMMAND?
Bash provides an environment variable called PROMPT_COMMAND. The contents of this variable are executed as a regular Bash command just before Bash displays a prompt.
Difference between .bash_profile and .bashrc
.bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.
When you login (type username and password) via console, either sitting at the machine, or remotely via ssh: .bash_profile is executed to configure your shell before the initial command prompt.
But, if you’ve already logged into your machine and open a new terminal window (xterm) then .bashrc is executed before the window command prompt. .bashrc is also run when you start a new bash instance by typing /bin/bash in a terminal.
On OS X, Terminal by default runs a login shell every time, so this is a little different to most other systems, but you can configure that in the preferences.
References
https://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x264.html
https://apple.stackexchange.com/questions/51036/what-is-the-difference-between-bash-profile-and-bashrc
As it was said here, to save git bash history on Windows you must not close the terminal with X button. Use exit command instead. History of commands will be saved then regardless of configuration mentioned in the accepted answer.
If you're using Git bash in VSCode please see C.M.'s comment above.
This worked for running git's bash in Visual Studio Code, but I had to put it ~/.bashrc not ~/.bash_profile. – C.M. Jul 29 at 14:43
This solved it for me.
There is a more complete answer on Unix Stackexchange, by Pablo R. and LinuxSecurityFreak. Add the following to your ~/.bashrc:
# Avoid duplicates
HISTCONTROL=ignoredups:erasedups
# When the shell exits, append to the history file instead of overwriting it
shopt -s histappend
# After each command, append to the history file and reread it
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"
Please be careful, though:
The problem with this PROMPT_COMMAND solution is that the numbers for each history item changes after each command. For example, if you type history(1) ls (2) rm, then you do !1 to repeat (1), the history number might change and might run the rm command. Chris Kimpton
If you use git bash for windows 8, just put this in your ~/.bash_logout file:
history > .bash_history
Obviously you need a ~/.bash_history file.
Regards.
For me what worked was going into C:\Users\MY_USER\ and deleting the .bash_profile file.
Ps: I am using windows 10
I am using Windows 10 for me it was a permission problem, my temporary solution was to add Everyone group and give it Full control on ~/.bash_history file.
By the way, for those using the Portable version of Git for Windows, there's no need to create .bash_profile or .bashrc. Simply add to C:<path to your Git Portable folder>\etc\bash.bashrc:
PROMPT_COMMAND='history -a'
Found an answer in another post : https://superuser.com/questions/555310/bash-save-history-without-exit
If you want to have an history updated between two terminals.
As a window user I created a file .bash_profile inside my user folder. And then I add the following content : PROMPT_COMMAND='history -a;history -c;history -r'
Just run this in your git bash
echo 'HISTFILE=$HOME/.bash_history' >> $HOME/.bashrc

Resources