zoukankan      html  css  js  c++  java
  • How to Change Your Default Shell on Linux with chsh

    How to Change Your Default Shell on Linux with chsh

    A stylized shell prompt on a Ubuntu-style Linux laptop system.Fatmawati Achmad Zaenuri/Shutterstock

    Bash isn’t the only Linux shell. It’s easy to try out other shells, like Zsh, which is very popular. When you’ve found one you like, use the chsh command to make it your default shell. We’ll show you how.

     
     

    Why a Shell Is Important

    The shell sits between you and the operating system. It provides the environment inside a terminal window that allows you to type commands and run programs. The shell checks your input and figures out what you want. If it can perform your bidding itself, it does so. If it needs outside help, it searches the path and finds the programs that can do whatever it is you requested.

    There are many different shells available on Linux. They all allow you to perform the same core tasks: explore the file system, work with files, launch programs, and run scripts. However, they each perform these tasks in their own way, and have their own special features and idiosyncrasies.

    Shells tend to be designed by people who want things to behave in a specific way. If your thinking aligns with that designer’s, that shell might well be a good fit for you. Plus, trying a new shell on Linux is easy.

    In most Linux distributions, including Ubuntu, the default shell is bash. It does a great job and is very capable. However, another shell might offer a time-saving difference that would have a big impact on your workflow. You’ll never know if you don’t look!

    RELATED: What is ZSH, and Why Should You Use It Instead of Bash?

    A Bucketful of Shells

    We’ve covered the different Linux shells before, but here’s a quick introduction to the most common:

    • bash: The Bourne again shell is the default in many distributions.
    • rbash: This Restricted bash shell provides minimal functionality to the person or script running in it.
    • ash: The Almquist shell is a lighter version of bash.
    • dash: The Debian Alquist Shell is the default shell script in Ubuntu. While bash is the default login and interactive shell, dash is used to run system processes because it’s much lighter than bash.
    • zsh: The Z shell is a modern take on the bash family of shells. It offers neat improvements, like command spellchecks and suggested corrections.
    • fish: This friendly interactive shell was written from scratch and isn’t derived from any of the other shell families. It’s intended to be user-friendly. Amongst its many other perks, fish offers suggestions for commands based on your history and the contents of the current folder, similar to predictive text.
    • ksh: The KornShell provides a particularly strong scripting language.

    RELATED: What's the Difference Between Bash, Zsh, and Other Linux Shells?

    List the Installed Shells

    To see which shells are installed on your computer, use this command. It simply lists the contents of the /etc/shells file:

    cat /etc/shells

    We’ve mentioned bashdash, and rbash, but what is sh?

    sh is the Thompson shell, written way back in 1971 by Ken Thompson of Bell Labs fame. It’s no longer maintained and has long since been superseded by modern shells. It’s included purely to maintain compatibility with older scripts that still have the following as their first line:

    #!/bin/sh

    This instructs the system to use the sh shell to execute the script. Do you really have that ancient shell on your machine, and is it being used to run your scripts? The which command will tell us which program actually runs when you type a command.

    Let’s see what runs when you type sh:

    which sh

    This appears to find a binary. if we dig a little deeper, though, we’ll see that it’s a symbolic link that actually points to dash, the lightweight shell used to execute scripts:

    ls -l /bin/sh

    That’s a neat, lightweight way to provide a safety net for scripts that expect to find sh on modern systems.

    Installing Another Shell

    Let’s install the fish shell and set it as the default for dave. On Ubuntu, we type the following command:

    sudo apt-get install fish

    On Manjaro, use pacman:

    sudo pacman -Sy fish

    On Fedora, type the following:

    sudo dnf install fish

    When the installation is complete, you can check the installed shells once more:

    cat /etc/shells

    Our new shell appears as /usr/bin/fish.  Take note of that path—you’ll need it shortly.

    The $SHELL Environment Variable

    The $SHELL environment variable holds the name of your current shell. We can check which one it’s set to with echo:

    echo $SHELL

    Let’s start the fish shell:

    fish

    Now, let’s check again what the $SHELL environment variable says:

    echo $SHELL

    The first time we use echo $SHELL, we’re in the bash shell. The environment variable holds the path to the bash executable, /bin/bash.

    When we launch the fish shell, we get a friendly welcome message and the command prompt changes. What might be surprising is the  $SHELL environment still holds the path to the bash executable, /bin/bash. That’s okay—this is normal.

    When you launch a new shell (or any other program), it inherits the environment of the parent shell. So, the fish shell inherits the global and exported environment variables from the bash shell. Because the value in the $SHELL environment variable hasn’t been changed, it has the same value in the fish shell that it did in the bash shell.

    We’re running fish like any other program. We can also use exit to exit the fish shell. It closes like any other program, and we return to the bash shell.

    That’s great for trying out new shells, seeing what they can do, and whether you get along with them. You can explore before you make the jump and adopt one as your go-to shell.

    If you decide to make the fish—or any other shell—your default, you’ll need to use the chsh command.

    The chsh Command

    The chsh command allows you to change your default shell. The trick is being aware that it allows you to change both the default login and default interactive shells. You might want to change one or the other, or both.

    Whenever you log in to obtain a command prompt, you use the shell configured to be your login shell. When you’re already logged in and open a terminal window, you use the shell configured to be your interactive shell. These can either be the same or different shells.

    To set your login shell, use chsh with no parameters:

    chsh

    You’re prompted for your password. Then, you must type the path to the new shell and hit Enter.

    If we make a remote connection to this test computer from another, we’ll find ourselves in the fish shell once we’ve logged in.

    To change your interactive shell use chsh with the -s (shell) option. Pass the path to the new shell on the command line:

    chsh -s /usr/bin/fish

    You’re prompted for your password and returned to the command prompt of your current shell. You need to log out and back in for the change to take effect. When you do, you’ll see the greeting and the fish shell command prompt.

    The $SHELL environment variable now holds the path to your new default shell:

    echo $SHELL

    Changing Another User Account’s Shell

    If you have root privileges and can use sudo, you can change the shells of other user accounts. The command is the same as before, with the addition of that person’s username added to the command line:

    sudo chsh -s /usr/bin/fish mary

    When mary next logs in, she’ll see the new shell when she opens a terminal window.

    Everybody Has a Favorite

    As long as you’re comfortable with your choice of shell, and it works for you, that’s great! Just remember, it must be able to run common scripts, such as installation routines. For the shells mentioned here, this shouldn’t be a problem.

    Of course, you can also download and install a new shell, and take it for a test drive without making any configuration changes to your computer. When you’re ready to tie the knot, chsh will perform the ceremony for you.

    Dave McKayDAVE MCKAY
    Dave McKay first used computers when punched paper tape was in vogue, and he has been programming ever since. After over 30 years in the IT industry, he is now a full-time technology journalist. During his career, he has worked as a freelance programmer, manager of an international software development team, an IT services project manager, and, most recently, as a Data Protection Officer. Dave is a Linux evangelist and open source advocate. READ FULL BIO »
  • 相关阅读:
    Django模型
    Django视图
    Django入门
    Python之安装第三方扩展库
    【转载】python计算文件的行数和读取某一行内容的实现方法
    easymock入门
    Head First Python之4持久存储
    Selenium2+python自动化之读取Excel数据(xlrd)
    Selenium2+python自动化之数据驱动(ddt)
    Head First Python之3文件与异常
  • 原文地址:https://www.cnblogs.com/mouseleo/p/14535830.html
Copyright © 2011-2022 走看看