Linux shell variable

Source: Internet
Author: User
Tags constant definition


Variable type
I always thought that the bash shell variables are non-typed and can also be specified.
Administrator@DADI ~$ declare -i num=10Administrator@DADI ~$ echo $num10Administrator@DADI ~$ num='iamaint'Administrator@DADI ~$ echo $num0

After the type is forcibly specified using the declare method, the variable does not receive non- Int.
Constant definition Syntax:
readonly OPTION VARIABLE(s)

After definition, the variable cannot be re-assigned or unset
Administrator@DADI ~$ readonly MY_NAME='Administrator'Administrator@DADI ~$ MY_NAME='ANOTHERNAME'-bash: MY_NAME: readonly variableAdministrator@DADI ~$ unset MY_NAME-bash: unset: MY_NAME: cannot unset: readonly variable


Two definition methods for Array Operations
ARRAY=(value1 value2 ... valueN)ARRAY[0]="no.1 element"

Element
myarray=(1 2 3 4 5 6 7 8)echo ${myarray[0]}myarray[0]="new value 1"

Number of array elements
echo ${#myarray[*]}
Do not confuse with the following. Only the length of the first element of myarray is returned.
echo ${#myarray}

Traversal
echo ${myarray[*]}for i in ${myarray[*]} ; do echo $i;done
Continuous length can also be used
array_len=${#myarray[*]}i=1while [ $i -lt $array_len ]do  echo ${myarray[$i]};  i=$[ $i + 1 ];donedone;

String operation substring
${VAR:OFFSET:LENGTH}

Administrator@DADI ~$ mystring="123456789"Administrator@DADI ~$Administrator@DADI ~$ echo ${mystring:2:2}34Administrator@DADI ~$ echo ${mystring:2:4}3456

Replace
${VAR/PATTERN/STRING}
$ echo ${mystring/2/new2}1new23456789




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.