zoukankan      html  css  js  c++  java
  • 【Linux 】理解bashrc和profile 的区别

    在一般的 linux 或者 unix 系统中, 都可以通过编辑 bashrc 和 profile 来设置用户的工作环境, 很多文章对于 profile 和 bashrc 也都有使用, 但究竟每个文件都有什么作用和该如何使用呢?

    常见操作系统中的bashrc和profile

    我们先来看一下常见的Linux系统中的bashrc和profile:

    在CentOs中:

    /etc/profile
    /etc/bashrc
    
    ~/.bashrc
    ~/.bash_profile

    在Ubuntu,Debian中:

    /etc/profile
    /etc/bash.bashrc
    
    ~/.bashrc
    ~/.profile

    Shell的种类

    在理解这些文件之前还需要了解Shell的 login(登入) 和 interactive(交互式) 模式.

    login shell 和 no-login shell

    Linux系统中shell分为很多种,比如bash,sh,zsh之类的,如果想要查看一个用户使用的是什么shell,可以通过finger [username]命令来查看。我们这里只说shell是bash的情况,因为如果是sh或者其他shell,显然不会运行bashrc的。

    “login shell” 代表用户登入, 比如使用 “su -” 命令, 或者用 ssh 连接到某一个服务器上, 都会使用该用户默认 shell 启动 login shell 模式,该模式下的shell会去自动执行 /etc/profile 和 ~/.profile 文件,但不会执行任何的bashrc文件,所以一般在 /etc/profile 或者 ~/.profile 里我们会手动去 source bashrc 文件。

    而 no-login shell 的情况是我们在终端下直接输入 bash 或者 bash -c “CMD” 来启动的 shell,该模式下是不会自动去运行任何的 profile 文件。

    interactive shell 和 non-interactive shell

    interactive shell 是交互式shell,顾名思义就是用来和用户交互的, 提供了命令提示符可以输入命令,该模式下会存在一个叫 PS1 的环境变量, 如果还不是 login shell 的则会去 source  /etc/bash.bashrc 和 ~/.bashrc 文件。
    non-interactive shell 则一般是通过 bash -c “CMD” 来执行的bash,该模式下不会执行任何的 rc 文件, 不过还存在一种特殊情况这个我之后详细讲述。

    关注公众号 海量干货等你
  • 相关阅读:
    【UR #17】滑稽树前做游戏
    chage
    [SDOI2016]储能表——数位DP
    password
    groupdel
    [NOI2017]泳池——概率DP+线性递推
    groupadd
    CF986C AND Graph
    userdel
    CF986C AND Graph
  • 原文地址:https://www.cnblogs.com/sowhat1412/p/12734267.html
Copyright © 2011-2022 走看看