Linux登录shell和非登录(交互式shell)环境变量配置
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Linux登录shell和非登录(交互式shell)环境变量配置,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3655字,纯文字阅读大概需要6分钟。
内容图文
![Linux登录shell和非登录(交互式shell)环境变量配置](/upload/InfoBanner/zyjiaocheng/1173/316d49ca00054cd3ac8dce57cd1b027e.jpg)
使用Jenkins执行shell脚本的时候, 碰到command not found
. 比如java mvn
, 这些环境变量配置在/etc/profile
中, 但jenkins执行的时候并没有加载.
这是因为jenkins执行的shell是非登录交互式shell, 并不会加载/etc/profile
.
交互式shell会加载.bashrc
, 进而会加载/etc/bashrc
, 而/etc/bashrc
会加载/etc/profile.d/*.sh
.
因此, 自定义的变量应该定义在/etc/profile.d/*.sh
1.登录shell
所谓登录shell,指的是当用户登录系统时所取的那个shell,登录shell属于交互式shell。
登录shell将查找4个不同的启动文件来处理其中的命令。 bash shell处理文件的顺序如下:
1:/etc/profile
2:/etc/profile.d等待配置文件
3:$HOME/.bash_profile 会加载$HOME/.bashrc和/etc/bashrc
4:$HOME/.bash_login
5:$HOME/.profile
2. 交互式非登录shell
如果启动了一个bash shell而没有登入系统(如在CLI提示符中键入bash),
则启动了一个交互式非登录shell.
$HOME/.bashrc
交互式非登录shell执行~/.bashrc文件中的命令.在每次执行shell脚本时,都会重新读取这个文件,所以是最完整的。
但是万事都不是一样的,debain系列的是不同的,如ubuntu
/etc/profile-->/etc/environment-->$HOME/.profile.要配置java等变量时,都/etc/environment中
.bashrc
# .bashrc
# User specific aliases and functions
alias rm=‘rm -i‘
alias cp=‘cp -i‘
alias mv=‘mv -i‘
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
登录shell的初始化文件(比如.bash_profile)通常会运行这个文件。这样,登录shell和非登录shell都可以使用.bashrc中的命令。
/etc/bashrc
[root@localhost ~]# cat /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" ]; then
if [ -z "$PROMPT_COMMAND" ]; then
case $TERM in
xterm*|vte*)
if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
elif [ "${VTE_VERSION:-0}" -ge 3405 ]; then
PROMPT_COMMAND="__vte_prompt_command"
else
PROMPT_COMMAND=‘printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"‘
fi
;;
screen*)
if [ -e /etc/sysconfig/bash-prompt-screen ]; then
PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
else
PROMPT_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
;;
esac
fi
# Turn on parallel history
shopt -s histappend
history -a
# Turn on checkwinsize
shopt -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
fi
if ! 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" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
}
# 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 file
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
umask 002
else
umask 022
fi
SHELL=/bin/bash
# Only display echos from profile.d scripts if we are no login shell
# and interactive - otherwise just process them to set envvars
for i in /etc/profile.d/*.sh; do
if [ -r "$i" ]; then
if [ "$PS1" ]; then
. "$i"
else
. "$i" >/dev/null
fi
fi
done
unset i
unset -f pathmunge
fi
# vim:ts=4:sw=4
参考
- https://www.cnblogs.com/woshimrf/p/shell-environment.html
- https://my.oschina.net/u/347414/blog/599054
原文:https://www.cnblogs.com/woshimrf/p/shell-conf.html
内容总结
以上是互联网集市为您收集整理的Linux登录shell和非登录(交互式shell)环境变量配置全部内容,希望文章能够帮你解决Linux登录shell和非登录(交互式shell)环境变量配置所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。