首页 > 系统相关 >【鸟哥 Linux 10.4】Linux Bash Shell 操作环境(包含通配符相关内容)

【鸟哥 Linux 10.4】Linux Bash Shell 操作环境(包含通配符相关内容)

时间:2022-08-16 21:17:41浏览次数:57  
标签:profile shell 10.4 bashrc etc Shell Linux login bash

【鸟哥 Linux 10.4】Linux Bash Shell 操作环境(包含通配符相关内容)

Linux 命令的路径搜寻顺序

  • 当我们在linux终端输入一行命令,系统是怎么直到我们要执行的是什么呢?其实系统是根据如下的顺序去查找输入的命令的:
  1. 以相对或者绝对路径指定的命令
  2. 通过alias找到的路径
  3. bash内建命令
  4. 通过$PATH环境变量找到的路径

Bash登录欢迎讯息:/etc/issue和/etc/motd

  • 当我们在终端登录的时候,会显示一段信息,这段信息配置在/etc/issue
  • 当需要在所有用户登录的时候显示一段信息,在/etc/motd中配置即可

Bash的环境配置文件


login shell和non-login shell

  • login shell:需要完整登录流程才能获取的shell称为login shell。在tty中登录获取的bash就是一种login shell。
  • non-login shell:不需要登录即可获取的shell成为non-login shell。比如:登录GUI之后打开GUI终端模拟器不需要进行登录操作,这就是一个non-login shell,如果在这个GUI终端中再使用bash命令开一个子shell,这个子bash也不需要登录,那么也是一个non-login shell。

login shell需要读取的配置文件

  • /etc/profile: 系统整体设定,最好不要修改。
  • ~/.bash_profile 或 ~/.bash_login 或 ~/.profile:个人设定,只会按照先后顺序读取其中一个。

/etc/profile文件

# 本/etc/profile文件摘取自linux5.4内核的linux mint发行版(基于ubuntu20)

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "${PS1-}" ]; then
  if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc # 执行外部配置数据
    fi  
  else
  # 根据用户显示不同的prompt sign,root用户(id为0)显示为#
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi  
  fi  
fi
# 执行/etc/profile.d文件夹中所有的shell script
if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi  
  done
  unset i
fi
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
  • 大体来说,默认的/etc/profile脚本属于系统配置,他将执行/etc/profile.d文件夹中的所有脚本。

~/.bash_profile文件

#对于ubuntu系统来说,没有~/.bash_profile文件,而是~/.profile文件,以下是该文件的内容。

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc" # 执行~/.bashrc用户自定义脚本
    fi  
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH" # 将$HOME/bin拼接到PATH环境变量中
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH" # 将$HOME/.local/bin拼接到PATH环境变量中
fi
  • 大体来说,~/.bash_profile 或 ~/.bash_login 或 ~/.profile负责将用户设定脚本~/.bashrc进行执行。

source刷新环境配置文件

  • 由于上述两种配置文件都属于login shell中的配置文件,修改过之后要重新登录才能刷新改动的配置。一个让配置文件的修改立即生效的方法就是使用source 配置文件名,一般用户使用source ~/.bashrc来立即更新用户配置文件。

  • source可以使用小数点代替,是一样的效果。这种方式很常见,在上面列出的两种配置文件内容中都有使用到。

~/.bashrc用户配置文件/

  • 当登录了non-login shell,就只会读取并执行~/.bashrc文件。
  • 如果不小心改坏了或者删除了这个文件,可以使用/etc/skel/.bashrc还原。推荐经常备份~/.bashrc

其他配置文件

/etc/man_db.conf

  • 存放man读取的数据的路径(ubuntu没有)

~/.bash_history

  • history命令读取的数据

~/.bash_logout

  • 配置用户注销之后,bash还需要执行哪些命令再真正离开。

stty(setting tty)和set

  • stty用于查看和设定当前tty下的按键映射
  • set用于显示所有用户变量和环境变量,另外也可以用于设定输入输出环境有关的一些配置。

通配符

符号 内容
# 注释
\ 转义
; 多命令分隔
~ 用户家目录
$ 取变量
& 让指令在后台工作
!
/ 路径分隔符
>,>> 输出数据流重导向,取代和累加
<,<< 输入数据流重导向,取代和累加
' ' 纯文本,单引号内不被shell解析
" " 保留shell解析
`?` 命令结果替换成文本
() 子shell
{} 命令区块

标签:profile,shell,10.4,bashrc,etc,Shell,Linux,login,bash
From: https://www.cnblogs.com/LeisureLak/p/16592974.html

相关文章

  • webRTC构建-Linux版
    linux下构建webRTC#!/bin/bashset-exu__CURRENT__=`pwd`__DIR__=$(cd"$(dirname"$0")";pwd)cd${__DIR__}start_time=$(date-u'+%Y-%m-%dT%H:%M:%SZ')star......
  • 【转】shell中各种括号的作用
    原文链接:https://blog.csdn.net/taiyang1987912/article/details/39551385一、小括号,圆括号()1、单小括号()①命令组。括号中的命令将会新开一个子shell顺序执行,所以括......
  • 【Azure 应用服务】如何从App Service for Linux 的环境中下载Container中非Home目录
    问题描述在AppServiceforLinux的环境中,我们能通过SSH进入到Container的环境中,并且可以通过在kudu站点的URL后面添加/newui打开一个适用于Linux环境的Kudu图形化页面,......
  • Linux--cheat命令详解
    今天给大家介绍一个Linux超级实用命令,有了这个命令,你就可以愉快使用Linux上几乎所有常用命令了,再也不用担心记不住那么多选项啦~我们知道,Linux系统总共有300~500个......
  • linux 中返回每一行中指定字符的所有列索引
     001、root@PC1:/home/test/test#lsa.txtroot@PC1:/home/test/test#cata.txt##测试数据abcdb......
  • Linux -服务器磁盘 Raid & 分区 & 挂载
     流计算服务器  流处理服务器 有一台流处理服务器(系统盘:2*600G、数据盘:6*600G)分区挂载如下: 设备名称分区大小挂载点文件系统类型磁盘用......
  • 使用Xshell连接阿里云无法输入password
    首先打开云服务器点击远程连接用vcn的连接方式去修改sshd的配置文件命令为:vi/etc/ssh/sshd_config按照官方给的错误提示进入编辑后发现参数没有问题于是乎我重......
  • linux 命令查看内存和cpu信息
    linux命令查看内存和cpu信息1、查看CPU信息命令cat/proc/cpuinfo2、使用top命令查看CPU的使用情况top3、free命令查看内存free4、df(diskfree):显示磁盘分区上......
  • MobaXterm链接linux虚拟机报错Network error: Connection refused
    原文链接使用虚拟机安装Ubuntu,然后在Windows下使用MobaXterm链接。若出现如下报错:Networkerror:Connectionrefused需要安装openssh:sudoaptinstallopens......
  • shell脚本echo打印错位
    问题描述在脚本中使用curl命令请求Jenkins的API获取job的编号,随后将编号和其他字符串拼接后,使用echo命令打印出来,但打印后字符串错位了。脚本大致如下:num=`curlAPI_URL......