
- shell 中的变量
- 参考文档
- 用户环境变量的更改
- Linux 中别名设置
- 变量的定义
- 变量定义的方法
- 变量的转译
- 变量的数组
- 传参
- 脚本函数
参考文档
- bash 内置变量官方文档:https://www.gnu.org/software/bash/manual/bash.html#Shell-Variables
- Shell Parameters:https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameters
- Shell 函数:https://www.gnu.org/software/bash/manual/bash.html#Shell-Functions
- 参考文章:https://www.runoob.com/linux/linux-shell-variable.html
用户环境变量的更改
- 环境变量:用户在 *** 作系统时使用到的命令搜索路径。
- 设定方式
[root@ ~]#vim ~/.bash_profile export PATH=$PATH:/mnt [root@ ~]#vim /etc/bash_profile export PATH=$PATH:/mnt
Linux 中别名设置
- 临时设定
[root@ shell_variable]# alias xie='vim' [root@ shell_variable]# which xie alias xie='vim' /bin/vim [root@ shell_variable]# - 只针对用户生效
[root@ shell_variable]# echo "alias xie='vim'" > ~/.bashrc [root@ shell_variable]# source ~/.bashrc [root@ shell_variable]# which xie alias xie='vim' /bin/vim [root@ shell_variable]# - 针对系统所有用户
[root@ shell_variable]# vim /etc/bashrc alias xie='vim [root@ shell_variable]#
- 删除当前环境中别名
[root@ shell_variable]# unalias xie [root@ shell_variable]#
- 重新加载文件
[root@ shell_variable]# source ~/.bashrc [root@ shell_variable]#
变量的定义
-
定义本身
- 变量就是内存一片区域的地址。
-
变量存在的意义
- 命令无法 *** 作一直变化的目标。
- 用一串固定的字符来表示不固定的目标可以解决此问题。
-
变量名称
- "字符" “_” "数字"
- 不能用数字开头
- 建议:
- 变量名称短全用大写字符
- 变量名称长用_区分子类
- WUHAHA
- Wuhaha_Linux
- wuhahA_Linux
变量定义的方法
-
环境级别,在环境关闭后变量失效。
export a=1
-
用户级别
vim ~/.bash_profile export a=1
-
系统级别
vim /etc/profile export a=2
vim /etc/profile.d/haha.sh export b=3 sh /etc/profile.d/haha.sh
-
【注】:
- export:声明环境变量。
- env:显示所有环境变量。
- echo $PATH:显示变量对应的值。
- unset variable_name:删除变量,unset 不能删除只读变量。
- 实例
[root@ ~]# export a=1 [root@ ~]# echo $a 1 [root@ ~]# env | grep a=1 a=1 [root@ ~]# unset a [root@ ~]# env | grep a=1 [root@ ~]# echo $a [root@ ~]#
变量的转译
-
转译
- / :转译单个字符
- " " :弱引用,批量转译个数字符 不能转译 “” 、"`" 、"$" 、"!"。可以出现变量、转译字符。
- ’ ’ :强引用,单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的。
-
声明
[root@ shell_variable]# a=1 [root@ shell_variable]# echo $a 1 [root@ shell_variable]# echo $ab [root@ shell_variable]# echo ${a}b 1b [root@ shell_variable]#
变量的数组
- 定义数组
[root@ shell_variable]# a=(1 2 3 4) [root@ shell_variable]#
- 打印数组的第一个元素
[root@ shell_variable]# echo ${a[0]} 1 [root@ shell_variable]# - 打印数组最后一个元素
[root@ shell_variable]# echo ${a[-1]} 4 [root@ shell_variable]# - 打印所有元素,显示为一串" 1 2 3 4 "
[root@ shell_variable]# echo ${a[*]} 1 2 3 4 [root@ shell_variable]# - 打印所有元素,显示为一个元素一串。“1” “2” “3” "4"
[root@ shell_variable]# echo ${a[@]} 1 2 3 4 [root@ shell_variable]# - 0 为起始元素,往后取 3 个。
[root@ shell_variable]# echo ${a[@]:0:3} 1 2 3 [root@ shell_variable]# - 数组中的元素个数。
[root@ shell_variable]# echo ${#a[@]} 4 [root@ shell_variable]#
传参
-
直接利用命令执行结果
- $ () | ``:优先执行
[root@ shell_variable]# TEST=`hostname` [root@ shell_variable]# echo $TEST wuhaha [root@ shell_variable]#
[root@ shell_variable]# TEST=$(hostname) [root@ shell_variable]# echo $TEST wuhaha [root@ shell_variable]#
-
非交互模式 脚本中的传参
- :脚本本身。
- :脚本后输入的第一串字符串。
- $* :脚本后输入的第二串字符串。
- $@ :脚本后所输入的所有字符"wuhaha linux redhat"。
- $# :脚本后所输入的所有字符’westos’ ‘linux’ ‘redhat’。
- #? :脚本后所输入的字符串个数。
- $$ :上一条命令的退出状态码。" 0 " -> " True "
[root@ shell_variable]# cat test.sh #!/bin/bash echo read test echo echo echo $* echo $@ echo $# echo $? echo $$ [root@ shell_variable]# sh test.sh wuhaha linux redhat test.sh wuhaha linux wuhaha linux redhat wuhaha linux redhat 3 0 16404 [root@ shell_variable]#
:表示当前进程的id。
-
非交互式传参
- read -p “please input password:” -s test :对 test 变量赋值。
[root@ shell_variable]# cat test.sh #!/bin/bash read test echo "===========" echo "echo $test" echo "###########" read -p "please input word:" test echo "===========" echo "echo $test" echo "###########" read -p "please input password:" -s test echo -e "necho $test" [root@ shell_variable]# sh test.sh this is test. =========== echo this is test. ########### please input word:this is test. =========== echo this is test. ########### please input password: echo this is test. [root@ shell_variable]#
:输出提示语。- 定义:程序的别名。 :隐藏输入内容。
[root@ shell_variable]# cat function.sh
#!/bin/bash
function test(){
echo
}
test linux
wuha() {
echo
}
wuha redhat
[root@ shell_variable]# sh function.sh
linux
redhat
[root@ shell_variable]#
脚本函数
- 有两种方式,都可以使用。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)