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

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"

Related

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.

Sending Bash Aliases to detached screen sessions

I'm on a Linux machine using screen, and I'm attempting to write a (fairly portable) function which runs a bash function in a new, detached screen session which automatically closes upon completion. I've had some success, but I noticed the following behavior:
If I include the definition of mail_submit() in my ~/.bashrc file, I can run
mail_submit foo
in the terminal, and also I can access the alias in a new screen session:
screen -S test
mail_submit foo
However, the following command does not work:
screen -d -m -S test sh -c 'mail_submit foo'
presumably because sh -c starts a fresh shell that has no knowledge of my ~/.bashrc profile. So, I can use the following fix:
screen -d -m -S test sh -c 'source ~/.bashrc; mail_submit foo'
which does work.
But if I want to wrap this functionality up into a bash alias (which is my ultimate goal here), this will cause a weird self-referential situation.
Question: What is an easy way to either have sh -c know the location of my ~/.bashrc profile, or use a variant of sourcing the file and creating an alias?
EDIT: I could save the shell script in my home directory, and create an alias which runs
screen -d -m -S test bash -c '~/mail_submit.sh $1'
but I'd still be curious to hear other possible fixes.
A default ~/.bashrc contains this ([[ "$-" != *i* ]] && return) little piece of code on top of it (or somewhere else in the upper part). This line will prevent the ~/.bashrc from beeing sourced if the bash shell doesn't run in interactive mode.
You could:
Remove this line
Create a new file which will only contain the alias you need and source that
Create a little bash script instead of an alias and run that
Do you mean screen -d -m -S test bash -c 'mail_submit foo'?
It looks like you're trying to run the command with the shell (sh), and not the bourne again shell (bash), which is the shell interpreter which actually reads the ~/.bashrc profile.
Edit: The .bashrc file is not being sourced by default because screen does not create the bash process as a login shell, which is when the .bashrc file is read. Creating a .screenrc file with the line defshell -bash will create the bash process as a login shell instead, which will then call the .bashrc file.

How to pbpaste in .bash_profile alias?

I try to make a small alias script to automatically reset my OS X Fuse connection.
If I run this command in my terminal:
pgrep sshfs|pbcopy; kill -9 $(pbpaste);echo my_password|pbcopy; sudo
umount path/to/my/mount/folder;
it works perfectly. If I however add it to my .bash_profile as an alias:
alias mount-reset="pgrep sshfs|pbcopy; kill -9 $(pbpaste);echo my_password|pbcopy; sudo umount path/to/my/mount/folder;"
It jumps over the pbpaste command and echoes my_password. I always thought that the alias commands are executed exactly as if I would write them to the terminal, but why is this not working? How do I pbpaste in alias script? What fundamentals have I understood wrong?
You need to wrap the alias in single-quotes, rather than double-quotes. The relevant difference is that bash expands $something (including $(command)) inside double-quotes before executing the command. The way you have it at the moment, pbpaste is executed when .bash_profile runs, and whatever happens to be in the paste buffer at that time gets included in the alias. With single-quotes, it includes $(pbpaste) directly in the alias, so it gets expanded when you use the alias.
alias mount-reset='pgrep sshfs|pbcopy; kill -9 $(pbpaste);echo my_password|pbcopy; sudo umount path/to/my/mount/folder;'
However, there's an easier way. I don't see any reason to pass the sshfs PID through the paste buffer; just use include it directly:
alias mount-reset='kill -9 $(pgrep sshfs);echo my_password|pbcopy; sudo umount path/to/my/mount/folder;'
...but that's still more complicated than it needs to be, because OS X includes a killall command which kills processes by name, thus eliminating the need to pgrep:
alias mount-reset='killall -9 sshfs;echo my_password|pbcopy; sudo umount path/to/my/mount/folder;'
(BTW, some unixes include a command named killall that does something much more dangerous -- this alias is not portable!)
In OS X alias commands should be added to ~/.bashrc
When bash is an interactive non-login shell it uses .bashrc, not .bash_profile. If bash is invoked as an interactive login shell, it uses .bash_profile, not .bashrc.
Try adding the alias to ~/.bashrc. If you wanted to add a mount-reset command as an interactive login shell command into ~/.bash_profile you could use something like this:
# kill sshfs
mount-reset () { pgrep sshfs|pbcopy; kill -9 $(pbpaste); echo my_password|pbcopy;
sudo umount path/to/my/mount/folder; }

Shell script using with tmux fails on zsh

I have following script:
#!/usr/bin/env bash
# set -xv
tmux new-window -n 'foo' 'source "$HOME/.rvm/scripts/rvm"; sleep 123' \;
On one machine it works perfectly, on the second I got an error:
sh: 1: source: not found
Ofcourse running command from shell works perfectly.
What is wrong? Machines have similar dot files....
source is not a POSIX command. Use . instead. The machine that fails is probably using dash as the system shell, not bash. The fact that tmux is executed from a bash script does not mean bash is used to execute the command given to new-window. tmux will use the system shell /bin/sh, so the command should not rely on non-POSIX features like the source synonym for ..

TAB completion and mc problems

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=8.10
DISTRIB_CODENAME=intrepid
DISTRIB_DESCRIPTION="Ubuntu 8.10"
This is the server version.
When I ssh into it, I encounter the following problems:
Problem 1
tab completion behaves weird to the point of being unusable:
> cd ~/<press TAB>
-sh: <( compgen -d -- '/home/dmitriid/' ): No such file or directory
> vi ~/.<press TAB>
<( compgen -d -- '/home/dmitriid/.' ): No such file or directory
-sh: <( eval compgen -f -X '*.#(o|so|so.!(conf)|a|rpm|gif|GIF|jp?(e)g|
JP?(E)G|mp3|MP3|mp?(e)g|MPG|avi|AVI|asf|ASF|ogg|OGG|class|CLASS)' --
$(quote_readline $cur) ): No such file or directory
> nano ~/.<press TAB>
./ .bash_logout .mc/ .viminfo
../ .bashrc .mysql_history
.aptitude/ .erlang.cookie .profile
.bash_history .gitconfig .ssh/
Is there a way to fix that?
Problem 2
I use mc quite a lot. I often do a Ctrl+O to hide panels and work in the shell. In my case:
Ctrl + O hides panels
Any keypress brings the panels back
Is there a way to fix that as well?
Thank you!
Ok. It turns out the solution was dumb and simple. The key was — no interactive shell in mc.
The answer lies here: http://www.ibiblio.org/mc/FAQ
6.6 When I use Ctrl-O I don't get a subshell. How do I fix this?
Only bash, tcsh and zsh can be used as subshell. Use one of those
shells as your default shell, and it will be used as subshell in GNU
Midnight Commander.
So I decided to change the shell:
> which bash
/bin/bash
> sudo chsh -s /bin/bash my_user_name
> grep ^my_user_name /etc/password
my_user_name:x:1002:1002::/home/my_user_name:/bin/bash
Note /bin/bash at the end of the passwd file. It means the shell is now changed.
After i logged out and then logged back in — voila, everything works!
I'd try to reinstall all bash packages, especially bash-completion: apt-get install --reinstall bash-completion, because it looks like some part of bash is screwed. Btw, I guess this should go to serverfault.com.
When I typed the following to switch to root, then it seemed that the permissions problem was resolved. Looks like compgen isn't able to read the directories it needs to when I'm logged in as an regular user.
sudo su -
"6.6 When I use Ctrl-O I don't get a subshell. How do I fix this?"
To use Ctr+o you should have SHELL=/bin/bash and not SHELL=/bin/sh
Add to .bashrc export SHELL=/bin/bash

Resources