KSH variable

本文详细介绍了KornShell(KSH)中的三种主要变量类型:函数变量、脚本/文件变量和系统环境变量,并探讨了它们的作用域、继承及共享方式。此外,还深入讲解了变量的设置、删除、数值型变量的声明及其属性设置方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

KSH Variable Category

  1. function variable
    1. It must be explicitly declared within a function using typeset command.
    2. It's valid only within the function it is declared.
  2. script/file variable
    1. It's defined in a file, and valid in that file.
    2. script/file variable is not valid in its callee script/file, until it's EXPORT-ed to environment.
  3. system environment variable
    1. environment can be shared anywhere.


All function variables, except those explicitly declared locally within the function with the typeset command, are inherited and shared by the calling Korn shell script.

For example:

# Function variable:
# Functions act almost just like external scripts, all variables are SHARED between the same ksh process.
# If a variable is changed inside a function, the variable value is changed after it has left the function.

VARA=AA
VARB=BB

function foo
{
  echo "1: VARA=$VARA,VARB=$VARB,VARC=$VARC"    # 1: VARA=AA,VARB=BB,VARC=
  eval $1=AF    # action VARA=AF
  VARB=BF
  VARC=CF
}

foo VARA

echo "2: VARA=$VARA,VARB=$VARB,VARC=$VARC"      # 2: VARA=AF,VARB=BF,VARC=CF

Variable Type

Basically, there is no data type concept inksh (except typeset -i, we will talk later), all values can be treated asstring; and when a numeric value is required , it can be converted dynamicallyas needed.



Variable Set

VAR_NAME1="ABC"

VAR_NAME2=ABC                        #same as “ABC”

VAR_NAME3="123"

VAR_NAME4=20                         # same as “20”, but it can be parsed asnumeric when needed.


VAR_ID1=NAME5

VAR_ID2=NAME6


eval VAR_${VAR_ID1}=true               # VAR_NAME5=true, it’s same as “true”

eval VAR_${VAR_ID2}=false              # VAR_NAME6=false, it’s same as “false”


VAR_NAME7=${VAR_NAME3}                    # VAR_NAME7=”123”

eval VAR_NAME8=\$\{VAR_${VAR_ID1}\}       # VAR_NAME8=true, the value of VAR_NAME5

Variable Unset 

unset VAR_NAME7                    # unsetVAR_NAME7

unset VAR_${VAR_ID1}             # unset VAR_NAME5


Numeric Variable 

let’s talk about “-i” attribute, which isused to declare a variable as numeric type.

typeset -i VAR_INT=12

typeset -i VAR_INT=”12”          # has same behavior as “12”, because string“12” was parsed as integer 12.


When a variable is declared as an integer,it can be calculated as:

VAR_INT=VAR_INT+5

Instead of

((VAR_INT=VAR_INT+5))


Variable Attribute(typeset)

typeset -i       Parameter is an  integer.

typeset -l       All upper-case characters are converted  to  lower-case.

typeset -L       Left justify and remove leading blanks from  value.

typeset -r       The given names are marked readonly and these names cannot be changed by subsequent assignment.

typeset -R       Right justify and fill with leading blanks.

typeset -u       All lower-case characters are converted  to  upper-case  characters.

typeset -x       The given names are marked for automatic export  to the environment of subsequently-executed commands.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值