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

前端 未结 8 825
再見小時候
再見小時候 2020-12-08 09:20

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 sho

相关标签:
8条回答
  • 2020-12-08 09:30

    the other answers fail with set -u

      if [ ! -z ${ZSH_VERSION+x} ]; then
        echo "this is zsh"
        echo ${(%):-%x}
      elif [ ! -z ${BASH_VERSION+x} ]; then
        echo "this is bash"
        echo $BASH_SOURCE
      else
        echo "not recognized"
      fi
    
    0 讨论(0)
  • 2020-12-08 09:32

    You can simply try

     echo $SHELL
    
    0 讨论(0)
  • 2020-12-08 09:34

    Just do echo $0 it says -zsh if it's zsh and -bash if it's bash

    EDIT: Sometimes it returns -zsh and sometimes zsh and the same with bash, idk why.

    0 讨论(0)
  • 2020-12-08 09:36

    Here is how I am doing it based on a previous answer from Gilles :

    if [ -n "$ZSH_VERSION" ]; then
      SHELL_PROFILE="$HOME/.zprofile"
    else
      SHELL_PROFILE="$HOME/.bash_profile"
    fi
    echo "export VAR1=whatever" >> $SHELL_PROFILE
    echo "INFO: Refreshing your shell profile: $SHELL_PROFILE"
    if [ -n "$ZSH_VERSION" ]; then
      exec zsh --login
    else
      source $SHELL_PROFILE
    fi
    
    0 讨论(0)
  • 2020-12-08 09:37

    A word of warning: the question you seem to have asked, the question you meant to ask, and the question you should have asked are three different things.

    “Which shell the user is using” is ambiguous. Your attempt looks like you're trying to determine which shell is executing your script. That's always going to be whatever you put in the #! line of the script, unless you meant your users to edit that script, so this isn't useful to you.

    What you meant to ask, I think, is what the user's favorite shell is. This can't be determined fully reliably, but you can cover most cases. Check the SHELL environment variable. If it contains fish, zsh, bash, ksh or tcsh, the user's favorite shell is probably that shell. However, this is the wrong question for your problem.

    Files like .bashrc, .zshrc, .cshrc and so on are shell initialization files. They are not the right place to define environment variables. An environment variable defined there would only be available in a terminal where the user launched that shell and not in programs started from a GUI. The definition would also override any customization the user may have done in a subsession.

    The right place to define an environment variable is in a session startup file. This is mostly unrelated to the user's choice of shell. Unfortunately, there's no single place to define environment variables. On a lot of systems, ~/.profile will work, but this is not universal. See https://unix.stackexchange.com/questions/4621/correctly-setting-environment and the other posts I link to there for a longer discussion.

    0 讨论(0)
  • 2020-12-08 09:38

    Myself having a similar problem, settled for:

    _shell="$(ps -p $$ --no-headers -o comm=)"                                                                                                       
    if [[ $_shell == "zsh" ]]; then                                                                                                                  
        read -q -s "?Do it?: "                                                                                                                    
    fi                                                                                                                                               
    elif [[ $_shell == "bash" || $_shell == "sh" ]]; then                                                                                              
        read -n 1 -s -p "Do it [y/n] "                                                                                                            
    fi                                                                                                                                               
    
    0 讨论(0)
提交回复
热议问题