如何查看shell环境变量

使用env命令查看所有环境变量:

[root@rhel6 tmp]# env
HOSTNAME=rhel6
TERM=vt100
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=192.168.180.1 3507 22
QTDIR=/usr/lib64/qt-3.3
QTINC=/usr/lib64/qt-3.3/include
SSH_TTY=/dev/pts/1
USER=root

修改bash提示符

  1. 查看现在的提示符设置:
[root@rhel6 ~]# echo $PS1
[\u@\h \W]$

可以看到\u对应root,\h对应主机名rhel6 ,\W 对应当前的目录~ , $对应#

  1. 我们试一下修改PS1的值
[root@rhel6 ~]# PS1='Hello>'
Hello>ls

使用shopt和set自定义Bash环境

现在让我们设置shell环境,编辑文件 ~/.bashrc:

# 纠正目录的拼写
shopt -q -s cdspell

# 当终端窗口大小调整时,更新显示。
shopt -q -s checkwinsize

# 打开扩展匹配模式
shopt -q -s extglob

# 在退出时追加而不是覆盖历史记录
shopt -s histappend

# 在历史记录中存放多行命令
shopt -q -s cmdhist 

# 在后台工作结束后马上进行通知
set -o notify 

# 禁用退出shell的[CTRL-D]
set -o ignoreeof

# 禁用 core 文件
ulimit -S -c 0 > /dev/null 2>&1

linux如何查找命令的位置

which命令将显示在当前环境中可执行的文件的路径名。

[root@rhel6 tmp]# which pwd
/bin/pwd
[root@rhel6 tmp]# which passwd
/usr/bin/passwd
[root@rhel6 tmp]# 

where命令用于定位命令的二进制文件、源文件和帮助文件。

[root@rhel6 tmp]# whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
[root@rhel6 tmp]# whereis pwd
pwd: /bin/pwd /usr/share/man/man1/pwd.1.gz /usr/share/man/man1p/pwd.1p.gz

whatis命令用于显示关于命令的简短描述。whatis命令将搜索手册页面名称,并显示命令的手册页面描述:

[root@rhel6 tmp]# whatis date
date                 (1)  - print or set the system date and time
date                 (1p)  - write the date and time
[root@rhel6 tmp]# whatis ifconfig
ifconfig             (8)  - configure a network interface
[root@rhel6 tmp]# whatis ping
ping                 (8)  - send ICMP ECHO_REQUEST to network hosts
[root@rhel6 tmp]# 

shopt变量

可以使用shopt命令打开或关闭控制可选行为的变量的值。

[root@rhel6 ~]# shopt
autocd          off
cdable_vars     off
cdspell         off
checkhash       off
checkjobs       off
checkwinsize    on
cmdhist         on
compat31        off
compat32        off
compat40        off
dirspell        off
dotglob         off
execfail        off
expand_aliases  on
extdebug        off
extglob         off
extquote        on
failglob        off
force_fignore   on
globstar        off
gnu_errfmt      off
histappend      off
histreedit      off
histverify      off
hostcomplete    on
huponexit       off
interactive_comments    on
lithist         off
login_shell     on
mailwarn        off
no_empty_cmd_completion off
nocaseglob      off
nocasematch     off
nullglob        off
progcomp        on
promptvars      on
restricted_shell        off
shift_verbose   off
sourcepath      on
xpg_echo        off
[root@rhel6 ~]# shopt -p
shopt -u autocd
shopt -u cdable_vars
shopt -u cdspell
shopt -u checkhash
shopt -u checkjobs
shopt -s checkwinsize
shopt -s cmdhist
shopt -u compat31
shopt -u compat32
shopt -u compat40
shopt -u dirspell
shopt -u dotglob
shopt -u execfail
shopt -s expand_aliases
shopt -u extdebug
shopt -u extglob
shopt -s extquote
shopt -u failglob
shopt -s force_fignore
shopt -u globstar
shopt -u gnu_errfmt
shopt -u histappend
shopt -u histreedit
shopt -u histverify
shopt -s hostcomplete
shopt -u huponexit
shopt -s interactive_comments
shopt -u lithist
shopt -s login_shell
shopt -u mailwarn
shopt -u no_empty_cmd_completion
shopt -u nocaseglob
shopt -u nocasematch
shopt -u nullglob
shopt -s progcomp
shopt -s promptvars
shopt -u restricted_shell
shopt -u shift_verbose
shopt -s sourcepath
shopt -u xpg_echo
[root@rhel6 ~]# 

如何启用(设置)和禁用(取消)每个选项?

shopt -s optionName # 设置
shopt -u optionName # 取消

示例

如果设置了cdspell选项,cd命令中目录名时会纠正拼写上的小错误。(多了一个字符,少了一个字符或者反了一个字符)例如

[root@rhel6 ~]# cd /etcc
-bash: cd: /etcc: No such file or directory
[root@rhel6 ~]# shopt -s cdspell
[root@rhel6 ~]# cd /etcc
/etc
[root@rhel6 etc]# cd /ect
/etc
[root@rhel6 etc]# 
自定义shell环境

严格来说,shell变量有两种类型:

  • 局部变量(shell变量,本地变量)-由shell和或用户脚本使用。所有用户创建的变量,除非使用export命令导出,都是本地的。
  • 环境变量—由shell或用户使用,但它们也被传递到其他命令。环境变量被传递给子进程或者子shell中。

Linux如何永久设置shell提示符?

修改 ~/.bashrc 或者 ~/.bash_profile

添加下面的内容

export PS1='\[\e[1;32m\][\u@\h \W]$\[\e[0m\] '

linux如何查看带点的文件?

使用ls的 -a 选项

ls -a

或者

ls -A | less

如何修改shell提示的颜色

给提示添加颜色很简单。将提示符设置为绿色:

export PS1='\[\e[1;32m\][\u@\h \W]$\[\e[0m\] '

设置红色

export PS1='\[\e[1;31m\][\u@\h \W]$\[\e[0m\] '

在SecureCRT设置颜色

在会话选项-》终端-》仿真中,勾选上ANSI颜色:
(如果是全局选项,先选择默认会话-> 编辑默认设置)

SecureCRT设置颜色

SecureCRT设置颜色

如何配置和自定义Bash shell环境?

Bash shell可以使用下面的内容进行配置:

  • 变量
  • set命令
  • shopt命令
  • 函数
  • 别名

shell提示符示例

编辑 ~/.bashrc文件 ,添加两个shell函数

bash_prompt_command() {
    # $PWD应该保留多少个字符
    local pwdmaxlen=25
    local trunc_symbol=".."
    local dir=${PWD##*/}
    pwdmaxlen=$(( ( pwdmaxlen < ${#dir} ) ? ${#dir} : pwdmaxlen ))
    NEW_PWD=${PWD/#$HOME/\~}
    local pwdoffset=$(( ${#NEW_PWD} - pwdmaxlen ))
    if [ ${pwdoffset} -gt "0" ]
    then
        NEW_PWD=${NEW_PWD:$pwdoffset:$pwdmaxlen}
        NEW_PWD=${trunc_symbol}/${NEW_PWD#*/}
    fi
}

bash_prompt() {
    case $TERM in
     xterm*|rxvt*)
         local TITLEBAR='\[3]0;\u:${NEW_PWD}
[root@rhel6 ~]# PROMPT_COMMAND="echo hello"
hello
[root@rhel6 ~]# date
Wed Apr  8 05:56:16 CST 2010
hello
[root@rhel6 ~]# 
7\]' ;; *) local TITLEBAR="" ;; esac local NONE="\[3[0m\]" # 重置终端的前景色 # 常规体+颜色 local K="\[3[0;30m\]" # 黑色 local R="\[3[0;31m\]" # 红色 local G="\[3[0;32m\]" # 绿色 local Y="\[3[0;33m\]" # 黄色 local B="\[3[0;34m\]" # 蓝色 local M="\[3[0;35m\]" # 品红色 local C="\[3[0;36m\]" # 青色 local W="\[3[0;37m\]" # 白色 # 粗体+颜色 local EMK="\[3[1;30m\]" local EMR="\[3[1;31m\]" local EMG="\[3[1;32m\]" local EMY="\[3[1;33m\]" local EMB="\[3[1;34m\]" local EMM="\[3[1;35m\]" local EMC="\[3[1;36m\]" local EMW="\[3[1;37m\]" # 背景色 local BGK="\[3[40m\]" local BGR="\[3[41m\]" local BGG="\[3[42m\]" local BGY="\[3[43m\]" local BGB="\[3[44m\]" local BGM="\[3[45m\]" local BGC="\[3[46m\]" local BGW="\[3[47m\]" local UC=$W # 普通用户使用的颜色 [ $UID -eq "0" ] && UC=$R # root使用的颜色 PS1="$TITLEBAR ${EMK}[${UC}\u${EMK}@${UC}\h ${EMB}${NEW_PWD}${EMK}]${UC}\$ ${NONE}" # 不适应颜色: PS1="[\u@\h ${NEW_PWD}]\$ " } # 设置PROMPT_COMMAND进行初始化 PROMPT_COMMAND=bash_prompt_command bash_prompt unset bash_prompt

PROMPT_COMMAND变量

如果设置了PROMPT_COMMAND环境变量,则在每个主提示符之前将该值作为命令执行。换句话说,在Bash显示提示符之前,该变量的内容作为常规Bash命令执行:

例如:

export EDITOR=/usr/bin/vim
# 导出 DISPLAY环境变量并运行xeyes 
export DISPLAY=localhost:11.0 xeyes

在每个提示符[root@rhel6 ~]#之前都执行了 PROMPT_COMMAND的命令。

取消:unset PROMPT_COMMAND

shell中如何导出局部变量?

使用export命令

vi  ~/.bashrc

使用别名

假设我们要创建别名来自定义bash shell环境:

别名系统将执行...
c清除屏幕
update在Debian系统上更新和安装最新版本的软件包。
ports列出PID相关进程的所有TCP/UDP监听端口。
vi运行vim而不是vi

那么我们可以这样做

  1. 编辑~/.bashrc文件
alias c='clear'
alias update='apt-get update && apt-get upgrade'
alias ports='netstat -tulpn'
alias vi='vim'
  1. 追加别名设置
. ~/.bashrc
或者
source ~/.bashrc
  1. 执行下面的命令,使别名生效
alias
  1. 查看所有别名
set

如何查看局部变量?

使用内置命令set来查看所有变量:

echo $SHELL
echo $MAIL

通常,所有大写的变量都是由bash设置的。例如:

PS1='\h \W $ '

自定义命令提示符

Bash shell允许通过插入反斜杠转义的特殊字符来定制提示字符串。

转义序列说明
\aASCII字符(07) 响铃
\dWeekday Month Date格式的日期(例如:“Tue May 26”)
\eASCII字符 ESC
\h主机名
\H主机名(FQDN)
\jshell管理的当前的任务数
\lshell的终端设备名的基本名
\n换行符
\r回车
\sshell的名称,$0的基本名(即最后一个斜线后面的部分)
\t当前时间(24小时制 格式为HH:MM:SS)
\T当前时间(12小时制 格式为HH:MM:SS)
\@当前时间(12小时制 格式为am/pm)
\A当前时间(24小时制 格式为HH:MM)
\u当前用户名
\vbash的版本(如2.00.0)
\V Tbash的发布版本+补丁级别(如2.00.0)
\w当前的工作目录,$HOME用波浪号缩写
\W当前工作目录的基本名,$HOME用波浪号缩写
\!命令的历史编号
\#命令的命令编号
\$如果UID是0(即用户root),则为#,否则为$
\nnn与八进制数字nnn相对应的字符
\\反斜杠
\[中括号[
\]中括号]

我们可以使用上述反斜杠转义序列来显示主机的名称和当前的工作目录:

[root@rhel6 ~]# set -o
allexport       off
braceexpand     on
emacs           on
errexit         off
errtrace        off
functrace       off
hashall         on
histexpand      on
history         on
ignoreeof       off
interactive-comments    on
keyword         off
monitor         on
noclobber       off
noexec          off
noglob          off
nolog           off
notify          off
nounset         off
onecmd          off
physical        off
pipefail        off
posix           off
privileged      off
verbose         off
vi              off
xtrace          off
[root@rhel6 ~]# 

设置shell选项

set和shopt命令用于控制shell的变量值。

查看当前配置的shell选项

set +o variableName

如何设置和取消shell变量的选项?

设置

set -o variableName

取消设置

set -o ignoreeof

示例

使用ctrl+d我们可以退出shell或者ssh会话

我们可以使用下面的命令禁止这样的操作

[root@rhel6 ~]# Use "logout" to leave the shell.
[root@rhel6 ~]# 

这时,再按 ctrl+d键,已经没法自己退出了。

##代码##
日期:2019-04-16 23:59:04 来源:oir作者:oir