# 變數和環境變數

### 一個變數可以通過以下方式進行賦值: var=value&#x20;

var是變量名,value是賦給變量的值。如果value不包含任何空白字符(例如空格),那麼*它不需要使用引號*進行引用,反之,則必須使用單引號或雙引號。

把var=value寫成var = value是一個常見的錯誤, 但前者是**賦值操作**,後者則是**相等操作**。

```
#!/bin/bash
#給變量var賦值 
var="value" 
echo $var 
echo ${var}
```

```
#!/bin/bash
#文件名:Variables.sh 
fruit=apple
count=5 
echo "We have $count ${fruit} (s)"
```

### **環境變數是沒在當前進程中定義,而從父進程中繼承而來的變數。**

**export命令用來設置環境變量。至此之後,從當前shell腳本執行的任何程序都會繼承這個變數。我們可以按照自己的需要,在執行的應用程序或者shell腳本中導出特定的變量。**

export AUTHOR="卓國興"

**vi use\_export.sh**

```
#!/bin/bash
echo "作者：${AUTHOR}"
echo "修改日期：`date +'%Y%m%d %H:%M'`"
```

在默認情況下, 有很多標準環境變量可供shell使用。 PATH就是其中之一。

環境變數PATH包含: echo $PATH


---

# Agent Instructions: 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:

```
GET https://kawsing.gitbook.io/opensystem/andoid-shou-ji/untitled-4/shell-script/bian-shu-he-huan-jing-bian-shu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
