一、变量介绍

将一些数据需要临时存放在内存中,以待后续使用时快速读出。

二、变量分类

1、本地变量:

用户私有变量,只有本用户可以使用,保存在家目录下的.bash_profile、.bashrc文件中

[root@localhost test20210724]# ls -a ~/.bash*
/root/.bash_history /root/.bash_logout /root/.bash_profile /root/.bashrc

2、全局变量:

所有用户都可以使用,保存在/etc/profile、/etc/bashrc文件中

[root@localhost test20210724]# ll /etc/profile /etc/bashrc -l
-rw-r--r--. 1 root root 2853 Apr 1 2020 /etc/bashrc
-rw-r--r--. 1 root root 1845 May 20 06:10 /etc/profile

3、用户自定义变量:

用户自定义,比如脚本中的变量

[root@localhost test20210724]# name='baism'
[root@localhost test20210724]# echo $name
baism

三、定义变量

1、变量格式:

变量名=值;在shell编程中变量名和等号之间不能有空格

2、变量命名规则:

(1)命名中只能使用英文字母、数字和下划线,首个字符不能以数字开发

(2)中间不能有空格,可以使用下划线

(3)不能使用标点符号

(4)不能使用bash里的关键字(可用help查看保留关键字)

注意:字符串要用单引号或双引号引起来

3、读取变量内容:echo $xx

[root@localhost test20210724]# name="小王"
[root@localhost test20210724]# age=18
[root@localhost test20210724]# echo 小王是$name,而他是$age岁
小王是小王,而他是18岁

4、取消变量:unset

[root@localhost test20210724]# name="小王"
[root@localhost test20210724]# unset name
[root@localhost test20210724]# echo $name

5、定义全局变量export

[root@localhost test20210724]# export gender='male'
[root@localhost test20210724]# echo $gender
male

6、定义永久变量

本地变量:用户私有变量,只有本用户可以使用,保存在家目录下的.bash_profile、.bashrc文件中

全局变量:所有用户都可以使用,保存在/etc/profile、/etc/bashrc文件中

(1)本地变量:

[root@localhost test20210724]# echo name='mrwhite' >> ~/.bash_profile
[root@localhost test20210724]# tail -1 ~/.bash_profile
name=mrwhite
[root@localhost test20210724]# echo $name [root@localhost test20210724]# source ~/.bash_profile
[root@localhost test20210724]# echo $name
mrwhite

(2)全局变量

[root@localhost test20210724]# echo "export age=30" >> /etc/profile
[root@localhost test20210724]# tail -1 /etc/profile
export age=30
[root@localhost test20210724]# echo $age [root@localhost test20210724]# source /etc/profile
[root@localhost test20210724]# echo $age
30

shell脚本(5)-shell变量的更多相关文章

  1. shell脚本介绍、shell脚本结构和执行、date命令用法、shell脚本中的变量

    7月11日任务 20.1 shell脚本介绍20.2 shell脚本结构和执行20.3 date命令用法20.4 shell脚本中的变量 20.1 shell脚本介绍 1.shell脚本语言是linu ...

  2. Linux centosVMware shell脚本介绍、shell脚本结构和执行、date命令用法、shell脚本中的变量

    一. shell脚本介绍 shell是一种脚本语言 aming_linux blog.lishiming.net 可以使用逻辑判断.循环等语法 可以自定义函数 shell是系统命令的集合 shell脚 ...

  3. shell脚本编程(一) 变量、条件判断、循环

    目录   1. shell脚本编程   2. 运行 Shell 脚本有两种方法   3. 变量   4. 本地变量   5. 环境变量   6. 参数变量   7. 多行注释   8. if条件判断 ...

  4. shell脚本学习—Shell执行脚本

    Shell作用是解释执行用户的命令,用户输入一条命令,Shell就解释执行这一条,这种方式称为交互式,但还有另一种执行命令的方式称为批处理方式,用户事先写一个Shell脚本,Shell可以一次把这些命 ...

  5. Shell脚本、Shell脚本结构、date命令的用法、变量

    1.Shell脚本: shell是一种脚本语言 目的:可以实现自动化运维,能大大增加运维的效率.2.Shell脚本结构:   #!/bin/bash  以#!/bin/bash开头,即以/bin/ba ...

  6. Shell脚本之:变量

    与编译型语言不同,shell脚本是一种解释型语言. 执行这类程序时,解释器(interpreter)需要读取我们编写的源代码(source code),并将其转换成目标代码(object code), ...

  7. shell脚本由基础变量及特殊变量($@、$*、$#等)到实战。

    一.shell脚本建立: shell脚本通常是在编辑器(如vi/vim)中编写,也可以在命令行中直接执行: 1.脚本开头:     规范的脚本第一行需要指出有哪个程序(解释器)来执行脚本中的内容,在L ...

  8. 利用shell脚本添加环境变量

    在shell脚本设置了环境变量,如export LIBRARY_PATH=./lib/,执行了此脚本后, 在执行生成的可执行文件,提示错误 error while loading shared lib ...

  9. linux学习18 shell脚本基础-bash变量和逻辑运行

    一.回顾 1.用户管理,权限管理,install,mktemp 2.用户管理: 3.权限管理: mode,ownership mode: user group other r w x 4.命令:ins ...

  10. Shell脚本之二 变量、字符串和数组

    一.Shell 变量 1.1 定义变量 定义变量时,变量名不加美元符号($),如: your_name="runoob.com" 注意,变量名和等号之间不能有空格,这可能和你熟悉的 ...

随机推荐

  1. Recommenders with TensorRT

    Recommenders with TensorRT 推荐系统用于向社交网络.媒体内容消费和电子商务平台的用户提供产品或媒体推荐.基于MLP的神经协作滤波器(NCF)推荐器使用一组完全连接或矩阵乘法层 ...

  2. Kaggle上的犬种识别(ImageNet Dogs)

    Kaggle上的犬种识别(ImageNet Dogs) Dog Breed Identification (ImageNet Dogs) on Kaggle 在本节中,将解决在Kaggle竞赛中的犬种 ...

  3. C++标准模板库(STL)——map常见用法详解

    map的定义 map<typename1, typename2> mp; map需要确定映射前类型和映射后类型,所以需要在<>内填写两个类型,第一个是键的类型,第二个是值的类型 ...

  4. python----日志模块loggin的使用,按日志级别分类写入文件

    1.日志的级别 日志一共分为5个等级,从低到高分别是: 级别 说明 DEBUG 输出详细的运行情况,主要用于调试. INFO 确认一切按预期运行,一般用于输出重要运行情况. WARNING 系统运行时 ...

  5. python+selenium基础篇,cookie绕过验证码小案例

    1.绕过验证码有许许多多的方法,笔者在此介绍其中的一个小方法,原理其实就是获取已登陆了之后的凭证(会话保存在本地的cookie中)再打开另一个页面的使用 2.F12(浏览器开发者调试工具)可以查看的需 ...

  6. vue3.0 props

    .orange { color: rgba(255, 165, 0, 1) } Vue3.0 props 1.你是否遇到了,引用props数据报错的问题? 在Vue3.0中,采用了proxy,让很多数 ...

  7. 【NX二次开发】获得屏幕矩阵并设置WCS为屏幕方向

    说明:获得屏幕矩阵并设置WCS为屏幕方向(Z朝向自己,X轴朝右,Y轴超上). 方法: 1 extern DllExport void ufusr(char *param, int *retcode, ...

  8. 大家看看大佬对Maven仓库的讲解,有何高明之处?

    Maven在某个统一的位置存储所有项目的共享的构件,这个统一的位置,我们就称之为仓库.(仓库就是存放依赖和插件的地方). 分类 maven的仓库只有两大类:1.本地仓库 2.远程仓库,在远程仓库中又分 ...

  9. noip2008 总结

    noip 2008题解 笨小猴 原题 笨小猴的词汇量很小,所以每次做英语选择题的时候都很头疼.但是他找到了一种方法,经试验证明,用这种方法去选择选项的时候选对的几率非常大!这种方法的具体描述如下:假设 ...

  10. asp.net core 实现 face recognition 使用 tensorflowjs(源代码)

    功能描述 上传照片文件名及是系统要识别标签或是照片的名称(人物标识) 提取照片脸部特征值(调用 facemesh模型) 保存特征值添加样本(调用 knnClassifier) 测试上传的图片是否识别正 ...