个人.bashrc:

~/.bashrc:

# .bashrc# User specific aliases and functionsalias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'# Source global definitions
if [ -f /etc/bashrc ]; then. /etc/bashrc
fi

全局bashrc:

/etc/bashrc:

# /etc/bashrc# System wide functions and aliases
# Environment stuff goes in /etc/profile# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.# are we an interactive shell?
if [ "$PS1" ]; thenif [ -z "$PROMPT_COMMAND" ]; thencase $TERM inxterm*|vte*)if [ -e /etc/sysconfig/bash-prompt-xterm ]; thenPROMPT_COMMAND=/etc/sysconfig/bash-prompt-xtermelif [ "${VTE_VERSION:-0}" -ge 3405 ]; thenPROMPT_COMMAND="__vte_prompt_command"elsePROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'fi;;screen*)if [ -e /etc/sysconfig/bash-prompt-screen ]; thenPROMPT_COMMAND=/etc/sysconfig/bash-prompt-screenelsePROMPT_COMMAND='printf "\033k%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'fi;;*)[ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default;;esacfi# Turn on parallel historyshopt -s histappendhistory -a# Turn on checkwinsizeshopt -s checkwinsize[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "# You might want to have e.g. tty in prompt (e.g. more virtual machines)# and console windows# If you want to do so, just add e.g.# if [ "$PS1" ]; then#   PS1="[\u@\h:\l \W]\\$ "# fi# to your custom modification shell script in /etc/profile.d/ directory
fiif ! shopt -q login_shell ; then # We're not a login shell# Need to redefine pathmunge, it get's undefined at the end of /etc/profile
    pathmunge () {case ":${PATH}:" in*:"$1":*);;*)if [ "$2" = "after" ] ; thenPATH=$PATH:$1elsePATH=$1:$PATHfiesac}# By default, we want umask to get set. This sets it for non-login shell.# Current threshold for system reserved uid/gids is 200# You could check uidgid reservation validity in# /usr/share/doc/setup-*/uidgid fileif [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; thenumask 002elseumask 022fiSHELL=/bin/bash# Only display echos from profile.d scripts if we are no login shell# and interactive - otherwise just process them to set envvarsfor i in /etc/profile.d/*.sh; doif [ -r "$i" ]; thenif [ "$PS1" ]; then. "$i"else. "$i" >/dev/nullfifidoneunset iunset -f pathmunge
fi
# vim:ts=4:sw=4

用户的bashprofile:

~/.bash_profile

# .bash_profile# Get the aliases and functions
if [ -f ~/.bashrc ]; then. ~/.bashrc
fi# User specific environment and startup programsPATH=$PATH:$HOME/binexport PATH

[root@ckl1 bashtest]# echo $HOME
/root

全局profile:

"/etc/profile"

# /etc/profile# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.pathmunge () {case ":${PATH}:" in*:"$1":*);;*)if [ "$2" = "after" ] ; thenPATH=$PATH:$1elsePATH=$1:$PATHfiesac
}if [ -x /usr/bin/id ]; thenif [ -z "$EUID" ]; then# ksh workaroundEUID=`/usr/bin/id -u`UID=`/usr/bin/id -ru`fiUSER="`/usr/bin/id -un`"LOGNAME=$USERMAIL="/var/spool/mail/$USER"
fi# Path manipulation
if [ "$EUID" = "0" ]; thenpathmunge /usr/sbinpathmunge /usr/local/sbin
elsepathmunge /usr/local/sbin afterpathmunge /usr/sbin after
fiHOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; thenexport HISTCONTROL=ignoreboth
elseexport HISTCONTROL=ignoredups
fiexport PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; thenumask 002
elseumask 022
fifor i in /etc/profile.d/*.sh ; doif [ -r "$i" ]; thenif [ "${-#*i}" != "$-" ]; then . "$i"else. "$i" >/dev/nullfifi
doneunset i
unset -f pathmunge#java environment
export JAVA_HOME=/usr/local/jdk1.8.0_161
export CLASSPATH=.:${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
export PATH=$PATH:${JAVA_HOME}/bin

关于别名:

别名,典型的如ll,是ls -l 的别名,那么这个别名定义在哪呢,其实是在/etc/profile.d/colorls.sh 中,也就是我在上面几个文件中,标红的部分。

colorls.sh:

# color-ls initialization# Skip all for noninteractive shells.
[ ! -t 0 ] && return#when USER_LS_COLORS defined do not override user LS_COLORS, but use them.
if [ -z "$USER_LS_COLORS" ]; thenalias ll='ls -l' 2>/dev/nullalias l.='ls -d .*' 2>/dev/nullINCLUDE=COLORS=for colors in "$HOME/.dir_colors.$TERM" "$HOME/.dircolors.$TERM" \"$HOME/.dir_colors" "$HOME/.dircolors"; do[ -e "$colors" ] && COLORS="$colors" && \INCLUDE="`/usr/bin/cat "$COLORS" | /usr/bin/grep '^INCLUDE' | /usr/bin/cut -d ' ' -f2-`" && \breakdone[ -z "$COLORS" ] && [ -e "/etc/DIR_COLORS.$TERM" ] && \COLORS="/etc/DIR_COLORS.$TERM"[ -z "$COLORS" ] && [ -e "/etc/DIR_COLORS.256color" ] && \[ "x`/usr/bin/tty -s && /usr/bin/tput colors 2>/dev/null`" = "x256" ] && \COLORS="/etc/DIR_COLORS.256color"[ -z "$COLORS" ] && [ -e "/etc/DIR_COLORS" ] && \COLORS="/etc/DIR_COLORS"# Existence of $COLORS already checked above.[ -n "$COLORS" ] || returnif [ -e "$INCLUDE" ];thenTMP="`/usr/bin/mktemp .colorlsXXX -q --tmpdir=/tmp`"[ -z "$TMP" ] && return/usr/bin/cat "$INCLUDE" >> $TMP/usr/bin/grep -v '^INCLUDE' "$COLORS" >> $TMPeval "`/usr/bin/dircolors --sh $TMP 2>/dev/null`"/usr/bin/rm -f $TMPelseeval "`/usr/bin/dircolors --sh $COLORS 2>/dev/null`"fi[ -z "$LS_COLORS" ] && return/usr/bin/grep -qi "^COLOR.*none" $COLORS >/dev/null 2>/dev/null && return
fiunset TMP COLORS INCLUDEalias ll='ls -l --color=auto' 2>/dev/null
alias l.='ls -d .* --color=auto' 2>/dev/null
alias ls='ls --color=auto' 2>/dev/null
(END)

View Code

所以,什么情况下可以执行别名呢:

1:执行了/etc/profile

2:执行了~/.bashrc,~/.bashrc中引用了 /etc/bashrc(在该文件中,执行了ll别名所在的colorls.sh文件)

3:执行了~/.bash_profile,因为该文件中判断是否存在~/.bashrc,存在的话,会去执行~/.bashrc;根据上一步的结论,自然也就可以执行别名。

ok。那么,进一步,什么情况会执行上述的两种情况:

第一种情况(/etc/profile):

查阅了man bash后,发现:

1.交互的登录shell,或者--login选项的非交互shell。

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes

commands from the file /etc/profile, if that file exists. After reading that file, it  looks for ~/.bash_profile, ~/.bash_login, and ~/.profile,

in that order, and reads and executes commands from the first one that exists and is readable.

The --noprofile option may be used when the shell is started to inhibit this behavior.

不过在我的centos7中,~/.profile不存在。

第二种情况(~/.bashrc):

1.交互的,但不是登录的shell。

When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists.

This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.

第三种情况(~/.bash_profile):

同情况1。

Shell的几种类型:

ok。那么看了上面两种还是很蒙,那么我们再科普下什么是交互shell、非交互shell、登录shell和非登录shell。

我是参考了这里:https://blog.csdn.net/wisgood/article/details/52043522

我的理解:

登录shell:

ssh登录的,就是登录shell,登录shell要退出的话,是执行logout。比如我们用的putty、securtCRT。

非登录shell:

登录成功了之后,或者并没有进行远程登录,直接就在本机的(VMVARE的虚拟机中的shell都不能算,可采用下面的方法测试)ubuntu打开终端,这种,我理解的就是非登录shell。

我做了个小实验,我是secureCRT远程到该服务器的,在/home/upload/bashtest/a.sh中,我写了句logout:

[root@ckl1 bashtest]# less a.sh
#/bin/bashlogout

执行该脚本:

[root@ckl1 bashtest]# ./a.sh 

结果:

./a.sh: line 3: logout: not login shell: use `exit'

区分登录与非登录shell的好办法:

(https://unix.stackexchange.com/questions/38175/difference-between-login-shell-and-non-login-shell)

1.通过echo $0,如果结果是-bash,那就是login shell。否则不是。

2.如果可以执行logout命令,那么就是login shell;否则不是。

交互shell:

就是我们不管是用SecurtCRT这样的工具进入远程服务器也好,或者本机打开终端也好,都可以执行各种命令

在执行这些命令时,shell是可以和我们互动的,比如要求我们输入东西,比如按Tab可以提示,等等。

非交互Shell:

shell脚本执行,一般来说就是非交互的,我们写好了脚本,只要交给shell执行就好,期间不需要和我们交互。命令输入错了,会直接提示并退出。

以上两种,其实是不同维度,可以两两组合

  交互 非交互
登录 远程登录ssh,调用的文件包括:/etc/profile,~/.bash_profile,~/.bash_login,~/.profile 较少见,可能读/etc/profile , ~/.profile。也可能不读。依然请参考第一个答案:https://unix.stackexchange.com/questions/38175/difference-between-login-shell-and-non-login-shell
非登录 本机打开终端,调用文件包括:~/.bashrc 执行shell脚本,调用文件包括:BASH_ENV 指定的文件

非交互shell中,怎么才能使用别名:

参考:

https://stackoverflow.com/questions/1615877/why-aliases-in-a-non-interactive-bash-shell-do-not-work

方式1:

shopt -s expand_aliases

不过我的centos 7,未生效。

方式2:

source their .bashrc at the end of their profile。

也就是在~/.bash_profile中新增一行:source ~/.bashrc

但是我这边依然没生效。

方式3:

直接脚本中增加:source ~/.bashrc

#/bin/bash
ls -lecho "ll "
source ~/.bashrcll

ok。可以正常工作。

ssh执行远程命令时,怎么才能使用别名:

1.

首先修改远程主机的~/.bashrc,新增一行:

shopt -s expand_aliases

再使用:

ssh -t @host ll

2.

ssh @host 'bash -ci ll'

参考:

https://stackoverflow.com/questions/1198378/ssh-command-execution-doesnt-consider-bashrc-bash-login-ssh-rc

https://stackoverflow.com/questions/1615877/why-aliases-in-a-non-interactive-bash-shell-do-not-work

转载于:https://www.cnblogs.com/grey-wolf/p/8891423.html

你可能不知道的shell、bash二三事(Centos 7)相关推荐

  1. 【coolshell酷壳】你可能不知道的Shell

    原文:http://coolshell.cn/articles/8619.html 你可能不知道的Shell Shell也叫做命令行界面,它是*nix操作系统下用户和计算机的交互界面.Shell这个词 ...

  2. 你可能不知道的Shell

    Shell也叫做命令行界面,它是*nix操作系统下用户和计算机的交互界面.Shell这个词是指操作系统中提供访问内核服务的程序. 这篇文章向大家介绍Shell一些非广为人知.但却实用有趣的知识,权当品 ...

  3. 【转】你可能不知道的Shell

    一些强大的命令 再分享一些可能你不知道的shell用法和脚本,简单&强大! 在阅读以下部分前,强烈建议读者打开一个shell实验,这些都不是shell教科书里的大路货哦:) !$ !$是一个特 ...

  4. axure设计出的原型打开自动跳转的问题_Axure原型设计中你不得不知道的技巧(二)...

    上篇讲了控件的使用技巧,相信读者还记忆犹新,这里再跟大家回顾下,控件使用技巧:1.滚动显示动态面板内容 2.如何画折线 3.整块的点击效果 4.输入框加上提示文字 5.连接流程图 6.优化导入图像.这 ...

  5. nginx location 匹配 多个规则_你需要知道的Nginx配置二三事

    Nginx是俄罗斯人Igor Sysoev基于C语言编写的十分轻量级的HTTP服务器,它主要有以下特点: 它是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器 ...

  6. 1N4148WS-E3-08你不得不知道的二三事

    2019独角兽企业重金招聘Python工程师标准>>> 1N4148WS-E3-08你不得不知道的二三事 特征 ·硅外延平面二极管 ·快速开关二极管 ·AEC-Q101合格 ·符合P ...

  7. 甘建二告诉你助理工程师职称的九大作用,你不得不知道的

    揭秘助理工程师职称的九大作用,你不得不知道的 助理工程师职称就是等于初级职称,就是叫法不一样,其实是一样的. 助理工程师职称是人才工作一段时间以后开始评审的第一个职称证,有人问助理工程师属于什么级别的 ...

  8. 自2018年以来我不知道的事情-Dan Abramov

    原文地址哈:https://overreacted.io/things-i-dont-know-as-of-2018/ 人们常常假设我知道的知识远比实际知识道的多.我不抱怨这个错误的想法.(拥有些许成 ...

  9. linux监控命令执行,你可能不知道的 即时监控 Linux 使用者执行指令的三种方法...

    原标题:你可能不知道的 即时监控 Linux 使用者执行指令的三种方法 这里介绍如何在 Linux 系统上以管理者权限即时监控一般使用者所执行的任何指令. Linux 的 root 管理者可对系统进行 ...

  10. 计算机的二三事——软件篇

    计算机的二三事--软件篇 写在前面 第一部分 操作系统 1.1 操作系统(OS) Windows MacOS Linux 1.2 操作系统的架构(x86(32位)/x64(64位)/arm(64位)) ...

最新文章

  1. Elasticsearch创建雇员目录
  2. python输出大小不同的字体_python – 为什么我的truetype字体大小为11渲染与windows不同?...
  3. vue-cli-plugin-element的使用
  4. freemarker常见语法大全
  5. 渝粤教育 陕西师范大学 《JAVA程序设计》作业
  6. docker删除镜像和删除容器
  7. java 关闭时_java – 活动关闭时服务停止
  8. PHP如何防止远程提交,PHP如何防止远程提交
  9. 华为发布企业服务云化转型战略 未来5年投资5亿美金建设服务能力(2017年09月06日)
  10. 数据泵避免个别表数据的导出
  11. apl脚本入门-控制语句
  12. OpenStack手动制作CentOS 7 KVM镜像
  13. Agile入门系列-1
  14. DOS批处理中对含有特殊字符的文件名的处理方法
  15. hiberante中get和load方法的区别
  16. java 判断两个时间相差的天数
  17. html5 指南针,14.html5 作业 简单移动端-指南针 用 canvas 画一下.
  18. android休眠状态,【Android休眠】之Android休眠机制
  19. ssh反向代理实现内网穿透;ssh+nginx实现公网云服务器代理访问内网服务器
  20. 黑客防线 2007~2012 NPM、PYPI、DockerHub 备份

热门文章

  1. MongoDB 学习笔记之 手动预先分片
  2. 走进javascript——解开switch之谜
  3. [SDOI2011]打地鼠
  4. 第五讲 python函数
  5. awl 多线程syn***
  6. 跟着锅子一步步学习32位汇编(3)---MOV和XCHG指令
  7. Java Persistence with MyBatis 3(中文版) 第二章 引导MyBatis
  8. 最近,前端开发把我恶心着了,为了甩锅,我写了个牛逼的日志切面!
  9. 推荐腾讯最新重磅开源项目!
  10. 一个 Java 方法,最多能定义多少参数?