Linux 中 ~/.profile 和 ~/.bashrc
一、主要内容理解 ~/.profile和~/.bashrc的区别知道他们是干什么的会一些实际应用。二、概念理解1./profile主要针对ssh等远程登录会话有效。2.bashrc主要针对当前本地登录的终端bash。3profile 和 bashrc 都是配置shell 终端的命令只是加载时机、适用场景、承载内容有严格区分3shell是一个壳里面有很多程序bash只是里面一种终端交互进程。shell里面常用的两个交互程序 1. bashBourne-Again ShellLinux 默认交互 Shell你日常用的 程序文件/bin/bash 定位sh 的超集完全兼容 sh增加大量独有功能 独有特性alias、自定义函数、[[ ]] 条件、数组、PS1 提示符、补全增强、source、~/.bashrc 使用场景桌面终端、SSH 远程登录、日常手动敲命令 配套配置.bashrc、.bash_profile 2. dashDebian Almquist ShellUbuntu 系统专用轻量 sh 程序文件/bin/dash 特点体积极小、启动速度飞快功能阉割只实现 POSIX 标准无 bash 扩展 Ubuntu 规则系统默认把 /bin/sh 链接到 dash所有开机、apt、服务启动脚本都用它加快开机速度 缺点不识别 alias、bash 数组、[[ ]] 等语法下面是使用ssh 远程登录 的查看进程所属关系目前进程使用的bash。pstree命令可查看 systemd─| ├─sshd─┬─sshd───sshd─┬─bash───pstree │ │ └─bash───sleep │ └─sshd───sshd───sftp-serverssh远程登录使用dash 命令 进入dash后//使用dash 零时进入dash ;再 pstree 命令 systemd─┬ ├─sshd─┬─sshd───sshd─┬─bash───dash───pstree │ │ └─bash───sleep │ └─sshd───sshd───sftp-server三、图示比较维度~/.profile~/.bashrc加载次数登录仅 1 次每开终端执行一次适用 shell所有 POSIX shellsh/bash/dash仅 bash存放内容全局环境变量 PATH、SDK 路径alias、函数、提示符、交互设置刷新命令source ~/.profile仅 SSH / 登录会话生效source ~/.bashrc新开终端立刻生效你容易踩坑放 alias 会失效非 bash 登录 shell 不认放大量 export 会重复加载拖慢终端启动四、修改用户前缀修改前PS1${debian_chroot:($debian_chroot)}\[\033[01;32m\]\u\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ 修改后PS1${debian_chroot:($debian_chroot)}\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ 修改内容去掉\h就会改变终端的显示效果修改前 kickpikickpi:~$ 修改后 kickpi:~$修改文件~/.bashrc #60 和 #62 if [ $color_prompt yes ]; then #PS1${debian_chroot:($debian_chroot)}\[\033[01;32m\]\u\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ PS1${debian_chroot:($debian_chroot)}\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ else PS1${debian_chroot:($debian_chroot)}\u:\w\$ #PS1${debian_chroot:($debian_chroot)}\u\h:\w\$ fi 立即生效命令 resource ~/.bashrc五、profile文件分析下面是默认的profile文件会加载~/.bashrc所以不管是先判断现在是不是 bash如果是 bash再检查有没有.bashrc文件存在就加载它if [ -n $BASH_VERSION ]; then # include .bashrc if it exists if [ -f $HOME/.bashrc ]; then . $HOME/.bashrc fi fi # set PATH so it includes users private bin if it exists if [ -d $HOME/bin ] ; then PATH$HOME/bin:$PATH fi # set PATH so it includes users private bin if it exists if [ -d $HOME/.local/bin ] ; then PATH$HOME/.local/bin:$PATH fi