Shell string array

Source: Internet
Author: User

Array Declaration Array

Declare-a Array_Name

Declare-a array_name: Associative array

Note: The two cannot be converted to each other

Array name and subscript (index)

Index: Numbering starting from 0, which is a numeric index

Note: Indexes can support the use of custom formats, not just numeric formats, which are associated with indexes, which are supported after the bash4.0 version

The array of Bash supports sparse format (index discontinuity)

Array Assignment

(1) Assign only one element at a time

Array_name[index]=value weekdays[0]= "Sunday" weekdays[4]= "Thursday"

(2) Assigning all elements at once

Array_name= ("VAL1" "VAL2" "VAL3" ...)

1 filename= ('ls /root/*. Sh ')23echo ${filename[@]} 4 /root/99.sh/root/a.sh/root/b.sh/root/caluid.sh/root/creat10user.sh/root/diskcheck.sh/root/g1.sh/root/ getuserinfo.sh/root/guess.sh/root/selectmenu.sh/root/shift.sh/root/testfor.sh/root/trap.sh/root/whiledf.sh 
View Code

(3) Assign only specific elements

Array_name= ([0]= "VAL1" [3]= "VAL2" ...)

(4) Interactive array value pairs assignment read-a array

referencing arrays

${array_name[index]} Note: Omitting [INDEX] means referencing an element with subscript 0

Referencing all elements of an array:

${array_name[*]} ${array_name[@]}

The length of the array (the number of elements in the array):

${#ARRAY_NAME [*]} ${#ARRAY_NAME [@]}

Delete an element in an array: Causes sparse format unset Array[index]

Delete entire array: unset array

Array slices

To reference an element in an array:

Array slice: ${array[@]:offset:number}

Offset: Number of elements to skip

Number: All elements after the offset of the element to be fetched ${array[@]:offset}

Append elements to the array: array[${#ARRAY [*]}]=value

String

Example of a string slice

1[[email protected] ~]# str= 'Echo{A.. z}|TR-D" "`2[Email protected] ~]#Echo$str3 abcdefghijklmnopqrstuvwxyz4[[email protected] ~]# str2= 'Echo{A.. Z} '5[Email protected] ~]#Echo$str 26A b c d e F g h i j k l m n o p q R s t u VWx y z7[Email protected] ~]#Echo${#str}8  -9[Email protected] ~]#Echo${#str2}Ten Wuyi One[Email protected] ~]#Echo${STR:3} A defghijklmnopqrstuvwxyz -[Email protected] ~]#Echo${STR:3:4} - DEFG the[Email protected] ~]#Echo${STR:-4} - WXYZ -[Email protected] ~]#Echo${STR:-4:-2} - WX +[Email protected] ~]#Echo${STR:-4:1} - W +[Email protected] ~]#Echo${STR:-4:3} AWxy
View Code

String pattern take substring

1[[email protected] ~]# line= 'Head-n1/etc/passwd`2[Email protected] ~]#Echo$line3ROOT:X:0:0: root:/root:/bin/Bash4[Email protected] ~]#Echo${line#*:}5X:0:0: root:/root:/bin/Bash6[Email protected] ~]#Echo${line##*:}7/bin/Bash8[Email protected] ~]#Echo${line%*:}9ROOT:X:0:0: root:/root:/bin/BashTen[Email protected] ~]#Echo${line%:*} OneROOT:X:0:0: root:/Root A[Email protected] ~]#Echo${line%%:*} - Root -[Email protected] ~]# disk="/dev/sda3 30G 82M 30G 1%/data" the[Email protected] ~]#Echo$disk -/dev/sda3 30G 82M 30G1% /Data -[Email protected] ~]#Echo${disk%% *} -/dev/Sda3 +[Email protected] ~]#
View Code

Search substitution for strings

${VAR/PATTERN/SUBSTR}: Finds the string that is represented by Var, the first time it is matched to by pattern, and replaces it with SUBSTR

${VAR//PATTERN/SUBSTR}: Find the string represented by Var, all strings that can be matched to by pattern, and replace it with SUBSTR

${var/#pattern/substr}: Finds the string that is represented by Var in the string that the beginning of the line is matched to by pattern, in order to substr replace the

${VAR/%PATTERN/SUBSTR}: Finds the string in the string represented by Var, where the end of the line is matched by pattern, to substr replace the

Search deletion of strings

${var/pattern}: Delete the string in the string represented by Var for the first time that the pattern is matched to

${var//pattern}: Removes all strings that are matched by pattern in the string represented by Var

${var/#pattern}: Removes all strings in the string represented by Var that match the beginning of pattern

${var/%pattern}: Removes all strings in the string represented by Var that match pattern to the end of the line

Character-Case conversions

${var^^}: Converts all lowercase letters in VAR to uppercase

${var,}: Converts all uppercase letters in VAR to lowercase

eval command

Eval first scans all the variables in the replacement command and then executes the replaced command

The eval command will first scan the command line for all permutations before executing the command. This command applies to variables that scan for a time that does not function. The command scans the variable two times

1[Email protected] ~]# n=Ten2[Email protected] ~]#Echo{1.. $n}3{1..Ten}4[Email protected] ~]# evalEcho{1.. $n}5 1 2 3 4 5 6 7 8 9 Ten6[Email protected] ~]# cmd=WhoAmI7[Email protected] ~]#Echo$CMD8 WhoAmI9[[Email protected] ~]# eval $CMDTen Root One[Email protected] ~]#
View Code

How to assign variable values

Shell string array

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.