环境变量和Bash内置命令

Command Line Editing

bash 复制代码
Ctrl+a#Move to the start of the line.(光标移到最前面)
Ctrl+e#Move to the end of the line.(光标移到最后面)
Ctrl+l#Clear the screen, reprinting the current line at the top.(不等同clear命令.会在顶部重新打印当前行,当前行还有内容时,还会显示)
#####demo
[root@kafka100 temp]# ls
heap.c
[root@kafka100 temp]# pwd
/root/workspace/cpp/temp
[root@kafka100 temp]# fdsfg
#####此时按下Ctrl+l,将变为下面,可以看到当前行的内容还在
[root@kafka100 temp]# fdsfg

环境变量

bash 复制代码
[root@kafka100 temp]# /root/workspace/cpp/temp/heap
Current heap end address: 0x15c8000
[root@kafka100 temp]# $(pwd)/heap
Current heap end address: 0x659000
[root@kafka100 temp]# ./heap
Current heap end address: 0x1960000
[root@kafka100 temp]# heap
bash: heap: command not found
# 原因:当前目录并不在PATH环境变量里
# 当可执行文件heap处于PATH环境变量的路径里时,执行的时候,就可以省去./了。
bash 复制代码
shell variable只有当前shell进程可以使用,当前shell的子进程访问不到

#使当前shell进程的子进程也可以使用
export TEST="test"
export [-fn] [-p] [name[=value]]
#使shell variable可以被当前shell的子进程使用
Mark each name to be passed to child processes in the environment.
If the -f option is supplied, the names refer to shell functions;otherwise the names refer to shell variables.
The -n option causes the export property to be removed from each name.
#让每个shell及其子进程,都能使用该环境变量,将export variable_name=value命令写到Bash的Startup files中

#If no options or arguments are supplied,set displays the names and values of all shell variables and functions.
set
#env和printenv的输出一样
printenv#列出所有的环境变量(If no VARIABLE is specified, prints the value of every environment variable.)
printenv JAVA_HOME KAFKA_HOME#Print the values of the specified environment VARIABLE(s).
env#Output the current environment.

Bash Variables

bash 复制代码
_:
At shell startup, set to the absolute pathname used to invoke the shell or shell script being executed as passed in the
environment or argument list.
Subsequently, expands to the last argument to the previous command, after expansion.
Also set to the full pathname used to invoke each command executed and placed in the environment exported to that command.
When checking mail, this parameter holds the name of the mail file currently being checked.

BASH_VERSION:Expands to a string describing the version of this instance of bash.
BASH_VERSINFO:A readonly array variable whose members hold version information for this instance of bash.
BASH_VERSINFO[0]#The major version number(the release).
BASH_VERSINFO[1]#The minor version number(the version).
BASH_VERSINFO[2]#The patch level.
BASH_VERSINFO[3]#The build version.
BASH_VERSINFO[4]#The release status(e.g., beta1).
BASH_VERSINFO[5]#The value of MACHTYPE.

HOSTNAME:the name of the current host.
HOSTTYPE:uniquely describes the type of machine on which bash is executing.
MACHTYPE:fully describes the system type on which bash is executing,in the standard GNU cpu-company-system format.

#每次打印提示符之前执行(bash5.1版本中可以是一个数组变量,此时每个元素包含的命令都会执行)
#Bash examines the value of the array variable PROMPT_COMMAND just before printing each primary prompt.
PROMPT_COMMAND:If set, the value is executed as a command prior to issuing each primary prompt.
#读取命令后,执行命令前(bash4.4版本才有的)
PS0:Expanded and displayed by interactive shells after reading a complete command but before executing it.
#提示符(就是每次输命令前面的[root@kafka100 cpp]#)
PS1:The primary prompt string.

BASHOPTS:A colon-separated list of enabled shell options.The options appearing in BASHOPTS are those reported as on by shopt.

Bash Builtin Commands

bash 复制代码
#Enable and disable builtin shell commands.
#Disabling a builtin allows a disk command which has the same name as a shell builtin to be executed without
#specifying a full pathname, even though the shell normally searches for builtins before disk commands.
#If -n is used, the names become disabled.Otherwise names are enabled.
#If the -p option is supplied, or no name arguments appear, a list of shell builtins is printed.
#The -a option means to list each builtin with an indication of whether or not it is enabled.
enable
enable -n test#禁用test内置命令(use the test binary found via $PATH instead of the shell builtin version)

#For each name, indicate how it would be interpreted if used as a command name.
#If the -t option is used, type prints a single word which is one of 'alias', 'function', 'builtin', 'file' or 'keyword',
#if name is an alias, shell function, shell builtin, disk file, or shell reserved word, respectively.
#If the -a option is used, type returns all of the places that contain an executable named file.
type [-afptP] [name ...]
type -t cat;type -t echo
type -a echo;type -a type;type -a ls;type -a for

whereis#locate the binary, source, and manual page files for a command
whereis echo#列出echo的二进制文件路径,源码文件路径,man手册文件路径

#Toggle the values of settings controlling optional shell behavior.
#With no options,or with the -p option,a list of all settable options is displayed,with an indication of whether
#or not each is set.
shopt

#Display current Readline key and function bindings, bind a key sequence to a Readline function or macro,
#or set a Readline variable.
bind -P >bind_output

https://wiki.calculate-linux.org/bash_shortcuts

https://www.gnu.org/software/bash/manual/bash.html

https://man7.org/linux/man-pages/man1/printenv.1.html

https://www.gnu.org/software/coreutils/manual/html_node/printenv-invocation.html

https://tiswww.case.edu/php/chet/bash/CHANGES

https://unix.stackexchange.com/questions/584003/what-does-the-export-command-do-to-a-variable

https://stackoverflow.com/questions/1158091/defining-a-variable-with-or-without-export

https://www.gnu.org/software/coreutils/manual/html_node/env-invocation.html#env-invocation

https://help.ubuntu.com/community/EnvironmentVariables

https://superuser.com/questions/881897/what-are-the-differences-between-sh-file-sh-and-source-file-sh

相关推荐
醉颜凉10 小时前
银河麒麟桌面操作系统修改默认Shell为Bash
运维·服务器·开发语言·bash·kylin·国产化·银河麒麟操作系统
HealthScience1 天前
怎么将bash(sh)的所有输出保存到log/txt中?
开发语言·bash
热心市民R先生2 天前
ros2 colcon build 构建后,install中的local_setup.bash 和setup.bash有什么区别
开发语言·机器人·bash
Robbie丨Yang2 天前
bash 脚本获取当前 git 所有本地分支
开发语言·git·bash
筑梦之路3 天前
直接用Bash发送HTTP请求 —— 筑梦之路
http·bash
Kika写代码4 天前
【已解决】【Hadoop】【./bin的使用】bash: ./bin/hdfs: 没有那个文件或目录
hadoop·hdfs·bash
DaphneOdera174 天前
问题解决实录 | bash 中 tmux 颜色显示不全
bash
Qlove957 天前
解决银河麒麟V10系统bash执行提示:无法执行:权限不够的问题
linux·bash
轻夏7 天前
WindowsTerminal中oh-my-posh样式的cmd、git-bash、cmder配置参数
开发语言·git·bash
Robbie丨Yang7 天前
【bash】删除本地所有分支
开发语言·chrome·bash