Skip to content

Different types of variables in Linux and Bash

1、本地变量

VAR=XYZ

2、局部变量

local VAR=XYZ

3、环境变量

在子shell中可以起作用

export VAR=XYZ

4、位置变量

$0  脚本名本身
$1  第一个参数
$2  第二个参数
...
${10}  第十个参数
${11}  第十一个参数
shift  依次向后移动参数

比如:分别显示第一、二、三个参数

!#/bin/bash
echo $1
shift
echo $1
shift
echo $1

5、特殊变量

$?    脚本执行状态(0--正确,1-255错误,1/2/127保留)
$#    参数的总个数
$*    参数列表
$@    参数列表

Disclaimer
  1. License under CC BY-NC 4.0
  2. Copyright issue feedback me#imzye.me, replace # with @
  3. Not all the commands and scripts are tested in production environment, use at your own risk
  4. No privacy information is collected here
Try iOS App