Unable to set bash 5.1 as default on Mac - bash

I installed bash-5.1 via Homebrew on my Macbook (M1-Monterey).
sachetz$ /opt/homebrew/bin/bash --version
GNU bash, version 5.1.16(1)-release (aarch64-apple-darwin21.1.0)
Added the path to /etc/shells file:
/bin/bash
/bin/csh
/bin/dash
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
/opt/homebrew/bin/bash
And ran the following command to set bash-5.1 as the default:
sachetz$ chsh -s /opt/homebrew/bin/bash
However, when I restart terminal and run the bash --version command, I still see bash-3.2:
sachetz$ bash --version
GNU bash, version 3.2.57(1)-release (arm64-apple-darwin21)
Copyright (C) 2007 Free Software Foundation, Inc.
But if I run echo $BASH_VERSION, I see bash-5.1
sachetz$ echo $BASH_VERSION
5.1.16(1)-release
Running which bash gives:
sachetz$ which bash
/bin/bash
Running echo $SHELL gives:
sachetz$ echo $SHELL
/opt/homebrew/bin/bash
When I run an echo $PATH with zsh as the default, I see /opt/homebrew/bin:
% echo $PATH
/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands
However, if I run the same after the chsh to Homebrew bash, I see:
sachetz$ echo $PATH
/Applications/XAMPP/xamppfiles/bin:/Applications/XAMPP/xamppfiles/bin/php:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands
I'm guessing the issue here is that /opt/homebrew/bin:/opt/homebrew/sbin is not present in the $PATH after I switch the default to Homebrew bash and restart the terminal. So the question is, why is the path variable different between the two, and how do I fix the issue?

Just needed to add /opt/homebrew/bin:/opt/homebrew/sbin: to the PATH variable in ~/.bash_profile like:
export PATH="/opt/homebrew/bin:/opt/homebrew/sbin:$PATH"
I suppose I missed this out as an installation step or something.

You might want to check the profiles, I believe bash checks both ~/.bashrc and ~/.bash_profile. Typically the $PATH are setup there.
For zsh, it looks at ~/.zshrc.
Also, you can check to see Terminal > Preference > Shells open with... option in the Terminal app.

which bash looks for bash under your path and bash --version is running a new bash from your path. If your path points to /bin/bash first, they'll give that bash's path/version. This isn't the correct way to verify the current shell's version.
The $BASH_VERSION & $SHELL point to the current shell and looks like it is already running the correct version.
You could also try new built-in variables, like
echo $EPOCHSECONDS

Related

Failed to activate virtualenv via shellscript

I'm trying to activate my pyenv-virtualenv environment through very simple bash script like this.
set -e
pyenv activate myenv
But I can't activate my env with below error:
Failed to activate virtualenv.
Perhaps pyenv-virtualenv has not been loaded into your shell properly.
Please restart current shell and try again.
I can activate it in command line, so I think it's due to my shell environment.
I use fish shell, but it's launched after $PATH settings read in bash shell.
I show you my settings.
.bash_profile:
export LANG="ja_JP.UTF-8"
export XDG_CONFIG_HOME="$HOME/.config"
export PATH="/sbin"
export PATH="/usr/sbin:$PATH"
export PATH="/bin:$PATH"
export PATH="/usr/bin:$PATH"
export PATH="/usr/local/bin:$PATH"
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
exec fish
And my config.fish:
status --is-interactive; and source (pyenv init -|psub)
status --is-interactive; and source (pyenv virtualenv-init -|psub)
What's wrong?
I'm using:
OS: OS X 10.13.5
fish: v2.7.1
pyenv: v1.2.5
pyenv-virtualenv: v1.1.3
I believe the cause is that you are initializing PyEnv in the Fish shell environment but then calling PyEnv in the Bash shell environment (via your Bash script). To solve this problem, you could try either of the following potential solutions:
Initialize PyEnv in your Bash environment (in addition to your Fish environment)
Write your script in Fish instead of Bash
Since PyEnv seems so have better support for Bash than Fish, let's focus on option #1. Step 3 of the PyEnv installation guide suggests the following command for adding PyEnv initialization to your ~/.bash_profile:
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
After running that command, I suspect your script will function as you expect. If it does not, you could try adding the same PyEnv initialization snippet (that you just added to your ~/.bash_profile) to the top of your Bash script.

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.

scl enable python27 bash

I've hit a snag with a shell script intended to run every 30 minutes in cron on a Redhat 6 server. The shell script is basically just a command to run a python script.
The native version python on the server is 2.6.6 but the python version required by this particular script is python 2.7+. I am able to easily run this on the command line by using the "scl" command (this example includes the python -V command to show the version change):
$ python -V
Python 2.6.6
$ scl enable python27 bash
$ python -V
Python 2.7.3
At this point I can run the python 2.7.3 scripts on the command line no problem.
Here's the snag.
When you issue the scl enable python27 bash command it starts a new bash shell session which (again) is fine for interactive commandline work. But when doing this inside a shell script, as soon as it runs the bash command, the script exits because of the new session.
Here's the shell script that is failing:
#!/bin/bash
cd /var/www/python/scripts/
scl enable python27 bash
python runAllUpserts.py >/dev/null 2>&1
It simply stops as soon as it hits line 4 because "bash" pops it out of the script and into a fresh bash shell. So it never sees the actual python command I need it to run.
Plus, if run every 30 minutes, this would add a new bash each time which is yet another problem.
I am reluctant to update the native python version on the server to 2.7.3 right now due to several reasons. The Redhat yum repos don't yet have python 2.7.3 and a manual install would be outside of the yum update system. From what I understand, yum itself runs on python 2.6.x.
Here's where I found the method for using scl
http://developerblog.redhat.com/2013/02/14/setting-up-django-and-python-2-7-on-red-hat-enterprise-6-the-easy-way/
Doing everything in one heredoc in the SCL environment is the best option, IMO:
scl enable python27 - << \EOF
cd /var/www/python/scripts/
python runAllUpserts.py >/dev/null 2>&1
EOF
Another way is to run just the second command (which is the only one that uses Python) in scl environment directly:
cd /var/www/python/scripts/
scl enable python27 "python runAllUpserts.py >/dev/null 2>&1"
scl enable python27 bash activates a python virtual environment.
You can do this from within a bash script by simply sourcing the enable script of the virtual environment, of the SCL package, which is located at /opt/rh/python27/enable
Example:
#!/bin/bash
cd /var/www/python/scripts/
source /opt/rh/python27/enable
python runAllUpserts.py >/dev/null 2>&1
Isn't the easiest to just your python script directly? test_python.py:
#!/usr/bin/env python
import sys
f = open('/tmp/pytest.log','w+')
f.write(sys.version)
f.write('\n')
f.close()
then in your crontab:
2 * * * * scl python27 enable $HOME/test_python.py
Make sure you make test_python.py executable.
Another alternative is to call a shell script that calls the python. test_python.sh:
#/bin/bash
python test_python.py
in your crontab:
2 * * * * scl python27 enable $HOME/test_python.sh
One liner
scl enable python27 'python runAllUpserts.py >/dev/null 2>&1'
I use it also with the devtoolsets on the CentOS 6.x
me#my_host:~/tmp# scl enable devtoolset-1.1 'gcc --version'
gcc (GCC) 4.7.2 20121015 (Red Hat 4.7.2-5)
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
scl is the dumbest "let us try and lock you in` nonsense I've seen in a while.
Here's how I made it so I could pass arguments to a series of scripts that all linked to a single skeleton file:
$ cat /usr/bin/skeleton
#!/bin/sh
tmp="$( mktemp )"
me="$( basename $0 )"
echo 'scl enable python27 - << \EOF' >> "${tmp}"
echo "python '/opt/rh/python27/root/usr/bin/${me}' $#" >> "${tmp}"
echo "EOF" >> "${tmp}"
sh "${tmp}"
rm "${tmp}"
So if there's a script you want to run that lives in, say, /opt/rh/python27/root/usr/bin/pepper you can do this:
# cd /usr/bin
# ln -s skeleton pepper
# pepper foo bar
and it should work as expected.
I've only seen this scl stuff once before and don't have ready access to a system with it installed. But I think it's just setting up PATH and some other environment variables in some way that vaguely similar to how they're done under virtualenv.
Perhaps changing the script to have the bash subprocess call python would work:
#!/bin/bash
cd /var/www/python/scripts/
(scl enable python27 bash -c "python runAllUpserts.py") >/dev/null 2>&1
The instance of python found on the subprocess bash's shell should be your 2.7.x copy ... and all the other environmental settings done by scl should be inherited thereby.

Switching from zsh to bash on OS X, and back again?

I'm learning to develop in Rails, and have discovered the power of zsh. However, for some of my other tasks, I wish to use normal bash.
Although they are the same, I just feel comfortable with the layout of bash in some situations.
How do I switch back and forth, or turn zsh on and off?
You can just use exec to replace your current shell with a new shell:
Switch to bash:
exec bash
Switch to zsh:
exec zsh
This won't affect new terminal windows or anything, but it's convenient.
you can try chsh -s /bin/bash to set the bash as the default,
or chsh -s /bin/zsh to set the zsh as the default.
Terminal will need a restart to take effect.
I switch between zsh and bash somewhat frequently. For a while, I used to have to source my bash_profile every switch. Then I found out you can (typically) do
exec bash --login
or just
exec bash -l
if it is just a temporary switch
you can use exec as mentioned above, but for more of a permanent solution.
you can use chsh -s /bin/bash (to switch to bash) and chsh -s /bin/zsh (to switch to zsh)
For Bash, try
chsh -s $(which bash)
For zsh, try
chsh -s $(which zsh)
In Mac OS Catalina default interactive shell is zsh.
To change shell to zsh from bash:
chsh -s /bin/zsh
Then you need to enter your Mac password. Quit the terminal and reopen it. To check whether it's changed successfully to ssh, issue the following command.
echo $SHELL
If the result is /bin/zsh, your task is completed.
To change it back to bash, issue the following command on terminal.
chsh -s /bin/bash
Verify it again using echo $SHELL. Then result should be /bin/bash.
zsh has a builtin command emulate which can emulate different shells by setting the appropriate options, although csh will never be fully emulated.
emulate bash
perform commands
emulate -R zsh
The -R flag restores all the options to their default values for that shell.
See: zsh manual
you can just type bash or if you always want to use bash:
on "iTerm2"
Go to preferences > Profiles > Command
Select "Command" from the dropdown
Type bash
Test by closing iTerm and open it again
You should be able just to type bash into the terminal to switch to bash, and then type zsh to switch to zsh. Works for me at least.
Follow the below steps !
chsh -s /bin/bash
Restart terminal
check which shell is in use by echo $SHELL
source .profile
You are back with Bash !!
For me, the solution was this:
Edit:
sudo vi /etc/passwd
Find your user, for me it was for example:
ubuntu:x:1000:1001::/home/ubuntu:/bin/sh
For you it might be:
ubuntu:x:1000:1001::/home/ubuntu:/bin/zsh
And change it to:
ubuntu:x:1000:1001::/home/ubuntu:/bin/bash
If you want bash to be defaul, or the line above if you want it to be zsh by default.
You can easily switch back to bash by using command "bye"

Resources