Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get shell to self-detect using zsh or bash

Tags:

bash

sh

zsh

I've a question on how to tell which shell the user is using. Suppose a script that if the user is using zsh, then put PATH to his .zshrc and if using bash should put in .bashrc. And set rvmrc accordingly.

#!/usr/bin/env bash export PATH='/usr/local/bin:$PATH' >> ~/.zshrc source ~/.zshrc 

I've tried the following but it does not work : (

if [[ $0 == "bash" ]]; then   export PATH='/usr/local/bin:$PATH' >> ~/.bashrc elif [[ $0 == "zsh" ]]; then   export PATH='/usr/local/bin:$PATH' >> ~/.zshrc fi  # ... more commands ...  if [[ $0 == "bash" ]]; then   [[ -s '/Users/`whoami`/.rvm/scripts/rvm' ]] && source '/Users/`whoami`/.rvm/scripts/rvm' >> ~/.bashrc   source ~/.bashrc elif [[ $0 == "zsh" ]]; then   [[ -s '/Users/`whoami`/.rvm/scripts/rvm' ]] && source '/Users/`whoami`/.rvm/scripts/rvm' >> ~/.zshrc   source ~/.zshrc fi 
like image 925
Juanito Fatas Avatar asked Mar 28 '12 15:03

Juanito Fatas


People also ask

Should I use zsh or bash?

Zsh is more interactive and customizable than Bash. Zsh has floating-point support that Bash does not possess. Hash data structures are supported in Zsh that are not present in Bash. The invocation features in Bash is better when comparing with Zsh.

How do I know if its zsh or bash?

Now, coming to the article's main subject, how will you know that you have bash or zsh? The answer is quite simple. Use the “–version” command to confirm the existence of both shells on your Linux system.

Can I use both bash and zsh?

Scripts are not affected. If you have a script in a file with execution permissions, starting with a shebang line such as #!/bin/bash or #!/bin/sh or #!/usr/bin/env bash , it'll keep working exactly as before. Zsh's syntax is not completely compatible with bash, but it's close.


1 Answers

If the shell is Zsh, the variable $ZSH_VERSION is defined. Likewise for Bash and $BASH_VERSION.

if [ -n "$ZSH_VERSION" ]; then    # assume Zsh elif [ -n "$BASH_VERSION" ]; then    # assume Bash else    # assume something else fi 

However, these variables only tell you which shell is being used to run the above code. So you would have to source this fragment in the user's shell.

As an alternative, you could use the $SHELL environment variable (which should contain absolute path to the user's preferred shell) and guess the shell from the value of that variable:

case $SHELL in */zsh)     # assume Zsh    ;; */bash)    # assume Bash    ;; *)    # assume something else esac 

Of course the above will fail when /bin/sh is a symlink to /bin/bash.

If you want to rely on $SHELL, it is safer to actually execute some code:

if [ -n "`$SHELL -c 'echo $ZSH_VERSION'`" ]; then    # assume Zsh elif [ -n "`$SHELL -c 'echo $BASH_VERSION'`" ]; then    # assume Bash else    # assume something else fi 

This last suggestion can be run from a script regardless of which shell is used to run the script.

like image 93
adl Avatar answered Sep 18 '22 08:09

adl