shell 变量

简介: <div class="markdown_views"><p>## 引言 <br>shell这门语言,作为与Linux交互效率最高的工具,我相信每个code monkey在工作中或多或少都会用到;我今天要讲的是这门语言中最基本的部分——变量。shell中的变量与类C语言差异较大,相信大家看完后都会有所收获。</p><h2 id="语法">语法</h2><p>在she

## 引言
shell这门语言,作为与Linux交互效率最高的工具,我相信每个code monkey在工作中或多或少都会用到;我今天要讲的是这门语言中最基本的部分——变量。shell中的变量与类C语言差异较大,相信大家看完后都会有所收获。

语法

在shell中,我们可以使用FOO=BAR这样的方式声明变量(注意,这里不能有空格),当使用这种方式声明变量时,变量是没有类型的,或者说变量的类型可以根据上下文自己转换。比如:


a=2334                   # Integer.
let "a += 1"
echo "a = $a "           # a = 2335
echo                     # Integer, still.


b=${a/23/BB}             # Substitute "BB" for "23".
                         # This transforms $b into a string.
echo "b = $b"            # b = BB35
declare -i b             # Declaring it an integer doesn't help.
echo "b = $b"            # b = BB35

let "b += 1"             # BB35 + 1
echo "b = $b"            # b = 1
echo                     # Bash sets the "integer value" of a string to 0.

c=BB34
echo "c = $c"            # c = BB34
d=${c/BB/23}             # Substitute "23" for "BB".
                         # This makes $d an integer.
echo "d = $d"            # d = 2334
let "d += 1"             # 2334 + 1
echo "d = $d"            # d = 2335

declare

我们可以使用shell内置的declare声明变量:

Option Meaning
-a Variable is an array.
-f Use function names only.
-i The variable is to be treated as an integer; arithmetic evaluation is performed when the variable is assigned a value (see Section 3.4.6).
-p Display the attributes and values of each variable. When -p is used, additional options are ignored.
-r Make variables read-only. These variables cannot then be assigned values by subsequent assignment statements, nor can they be unset.
-t Give each variable the trace attribute.
-x Mark each variable for export to subsequent commands via the environment.

shell支持三种数据类型:字符串、整型、数组。

字符串类型

字符串类型是shell声明一个变量时默认的类型,当我们执行

JAVA_HOME=/usr/default/java

时,JAVA_HOME这个变量的类型是string,

语法

语法 说明
${parameter:-defaultValue} Get default shell variables value
${parameter:=defaultValue} Set default shell variables value
${parameter:?”Error Message”} Display an error message if parameter is not set
${#var} Find the length of the string
${var%pattern} Remove from shortest rear (end) pattern
${var%%pattern} Remove from longest rear (end) pattern
${var:num1:num2} Substring
${var#pattern} Remove from shortest front pattern
${var##pattern} Remove from longest front pattern
${var/pattern/string} Find and replace (only replace first occurrence)
${var//pattern/string} Find and replace all occurrences

参考

  1. HowTo: Use Bash Parameter Substitution Like A Pro
  2. Bash Variables Are Untyped
  3. Types of variables
目录
相关文章
|
4月前
|
Shell Linux
|
4月前
|
Shell Python
python 和shell 变量互相传递
python 和shell 变量互相传递
48 0
|
4月前
|
Shell Linux
Linux下的Shell基础——变量、运算符、条件判断(二)
Linux下的Shell基础——变量、运算符、条件判断(二)
90 0
|
9天前
|
Java Shell Linux
Shell 变量设置
Shell设置变量
31 5
|
10天前
|
Java Shell Linux
Shell 变量设置25-1
Shell编程语言是一种非类型的解释型语言,无需像C++/Java那样事先声明变量。通过赋值即可定义变量,在Linux支持的所有Shell中均适用。变量分为局部变量与环境变量,前者仅限于定义脚本内使用,后者可在其派生的子进程中使用。常见系统变量如$0表示当前程序名称,$n表示第n个参数(n=1,2,...,9),$*代表所有参数,$#代表参数个数,$?表示命令执行后的状态(0为成功),$UID为当前用户ID,$PWD表示当前目录。定义变量如`A=123`,
21 0
|
1月前
|
Shell
[shell]在curl测试的data参数中引用变量
[shell]在curl测试的data参数中引用变量
102 1
|
2月前
|
分布式计算 大数据 Shell
MaxCompute产品使用合集之odps shell如何将ech变量的结果集合写入文件,并且指定服务器的位置
MaxCompute作为一款全面的大数据处理平台,广泛应用于各类大数据分析、数据挖掘、BI及机器学习场景。掌握其核心功能、熟练操作流程、遵循最佳实践,可以帮助用户高效、安全地管理和利用海量数据。以下是一个关于MaxCompute产品使用的合集,涵盖了其核心功能、应用场景、操作流程以及最佳实践等内容。
35 10
|
4月前
|
Shell Linux Perl
Linux|如何允许 awk 使用 Shell 变量
Linux|如何允许 awk 使用 Shell 变量
68 2
|
2月前
|
Shell Linux
Linux环境变量之shell中export定义全局变量和echo 变量的区别
Linux环境变量之shell中export定义全局变量和echo 变量的区别
|
3月前
|
存储 Shell 开发者
Shell 变量详解:如何定义、使用和管理
**Shell脚本中,变量是核心元素,用于暂存数据。变量默认为字符串,赋值时等号两侧无空格。命名遵循:数字、字母、下划线,以非数字开头。常用特殊变量如 `$0` (脚本名),`$#` (参数个数)。定义变量可使用单引号(不解析变量)、双引号(解析变量)或不加引号(注意空格)。使用花括号明确变量边界,`readonly` 则可设定只读变量,`unset` 删除变量。实践这些概念以加深理解。**
146 0