Common techniques for learning array in shell scripts

Source: Internet
Author: User
The subscript of the array in shell is 1 starting from 0 by default. Put the string in the array and get its length #! /Bin/bashstr & quot; AB -- nd & quot; array ($ str) length $ {# array [@]} echo $ lengthfor (i0; I & lt; $ length; I ++) doecho $ {array [$ I]} done execution...

The subscript of the array in shell starts from 0 by default.

1. Put the string in an array to obtain its length.

#! /Bin/bash
Str = "a B -- n d"
Array = ($ str)
Length =$ {# array [@]}
Echo $ length

For (I = 0; I <$ length; I ++ ))
Do
Echo $ {array [$ I]}
Done

Execution result:

[Oracle @ 99bill-as9 array] $ sh length. sh
4
A
B
-- N
D

Print string:

#! /Bin/bash
Str = "a B c"
For I in $ str
Do
Echo $ I
Done

Or:

#! /Bin/bash
Str = "a B c"
Array = ($ str)
For (I = 0; I <$ {# array [@]}; I ++ ))
Do
Echo $ {array [$ I]}
Done

Execution result:

A
B
C

2. When a string is separated by other characters

#! /Bin/bash

Str2 = "a # B # c"
A = ($ (echo $ str2 | tr '# ''' | tr-s ''))
Length =$ {# a [@]}

For (I = 0; I <$ length; I ++ ))
Do
Echo $ {a [$ I]}
Done
# Echo $ {a [2]}

Execution result:

A
B
C

3. Other operations on arrays

#! /Bin/bash
Str = "a B -- n dd"
Array = ($ str)
Length =$ {# array [@]}

# Ouput the first array element outputs the first element of the array.
Echo $ array

# Use subscript way access array access to array elements using the following method
Echo $ {array [1]}

# Output the array to Output this array
Echo $ {array [@]}

# Output in the array subscript for 3 the length of the element Output the length of the element marked as 3 in the array
Echo $ {# array [3]}

# Output in the array subscript 1 to 3 element: elements marked as 1 to 3 in the Output array
Echo $ {array [@]: 1: 3}

# Output in the array subscript greater than 2 elements Output the element whose subscript is greater than 2
Echo $ {array [@]: 2}

# Output in the array subscript less than 2 elements Output the element whose subscript is less than 2 in the array
Echo $ {array [@]: 2}

Execution result:

A
B
A B -- n dd
2
B -- n dd
-- N dd
A B

4. Traverse to access a string (separated by spaces by default. when the string is separated by other delimiters, refer to 2)

#! /Bin/bash
Str = "a -- m"
For I in $ str
Do
Echo $ I
Done

Execution result:

A
-- M

5. How to use echo to output a String str = "-n". because-n is a parameter of echo, the general echo "$ str" method cannot be output.
The solution can be:


Echo x $ str | sed's/^ x //'
Echo-ne "$ str \ n"
Echo-e "$ str \ n \ c"
Printf "% s \ n" $ str (this can also be done)

Author: "Zhu Ying's column"

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.