> For the complete documentation index, see [llms.txt](https://beret81.gitbook.io/study-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://beret81.gitbook.io/study-notes/shell-jiao-ben/shell-bian-liang.md).

# shell 变量

### 一、变量介绍

```
在编程中，我们总有一些数据需要临时存放在内存，以待后续使用时快速读出。
内存在系统启动的时候被按照1B一个单位划分为若干个块，然后统一   
编号(16进制编号)，并对内存的使用情况做记录，保存在内存跟踪表中。
```

那么问题来了，1G内存有多少个1B的单位呢？

```
计算机的单位:
1B=8bit
1KB=1024B
1MB=1024KB
1GB=1024MB
1TB=1024GB
1PB=1024TB
1EB=1024PB
1ZB=1024EB
...
好了，已经够大了！当然还有YB、BB更大的单位，同样进制也是1024.
1G=1024*1024*1024=1073741824B
```

假如你将一个1B的字符存入内存，如何读出呢？有没有一种大海捞针的感觉啊！我们讨论一下计算机是如何通过让我们人类快速将数据存在内存，如何从内存中读出数据的。我们研究过变量后就明白了。

变量：变量是编程中最常用的一种临时在内存中存取数据的一种方式。

变量存取原理

![](https://book.apeland.cn/media/images/2019/05/09/kcxvft.png)

从图片可以看出，当我们在脚本中定义变量存值的时候，可以从以下方面看到变化：

1）内存占用：如果存的是一个字符则占用1个字节，如果存的是字符串则是字符串的长度加1个字节长度(\0是一个特殊字符，代表字符串结束)。

2）变量名与内存空间关系：计算机中会将对应的内存空间和变量名称绑定在一起，此时代表这段内存空间已经被程序占用，其他程序不可复用；然后将变量名对应的值存在对应内存地址的空间里。

```
理解变量存储：STRING1="ABC"
1）STRING1(逻辑地址) <===> 0X5...0X8(物理地址)      存取数据 ABC
STRING1是给人看的，方便人记忆；
0x5...0x8是内存物理地址，是计算机寻址的依据；
2）对于人来说STRING1上存的数据是ABC，对于计算机来说数据是存在物理地址上的；
3）在建立变量的时候计算机自动将逻辑地址(变量名)和物理地址做了对应。
变量读出
1）当调用STRING1的时候，计算机会根据对应关系，找到物理地址
2）定位内存地址，读出数据并返回
```

### 二、变量分类

本地变量：用户私有变量，只有本用户可以使用，保存在家目录下的.bash\_profile、.bashrc文件中

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

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

### 三、定义变量

#### 2.1）定义变量

变量格式： 变量名=值

在shell编程中的变量名和等号之间不能有空格。

```
变量名命名规则：
    命名只能使用英文字母，数字和下划线，首个字符不能以数字开头。
    中间不能有空格，可以使用下划线（_）。
    不能使用标点符号。
    不能使用bash里的关键字（可用help命令查看保留关键字）。
```

VAR1=1

age=18

name=’baism’

score=88.8

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

定义变量演示：

```
变量赋值，此种方法设置为本地变量
[root@www ~]# name="baism"
[root@www ~]# school='ayitula'
[root@www ~]# age=30
[root@www ~]# score=88.8
```

#### 2.2）读取变量内容

读取变量内容符:$

读取方法：$变量名

```
变量内容读出
[root@www ~]# echo $name
baism
[root@www ~]# echo $school
ayitula
[root@www ~]# echo $age
30
[root@www ~]# echo $score
88.8
```

#### 2.3）取消变量 unset

```
[root@www ~]# unset name
[root@www ~]# echo $name
```

#### 2.4）定义全局变量 export

```
[root@www ~]# export name='baism'
上述设置的变量其实都是一次性变量，系统重启就会丢失。
如果希望本地变量或者全局变量可以永久使用，可以将需要设置的变量写入变量文件中即可。
```

#### 2.5）定义永久变量

本地变量：用户私有变量，只有本用户可以使用，保存在家目录下的.bash\_profile、.bashrc文件中

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

```
本地变量
[root@www ~]# tail -1 ~/.bash_profile
name='baism'
全局变量
[root@www ~]# tail -1 /etc/profile
export age=30
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://beret81.gitbook.io/study-notes/shell-jiao-ben/shell-bian-liang.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
