Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Shell array: Define Shell array, the array length     - Understanding and Memcached MongoDB arbitration node, Zookeeper, Redis Recovery Programme Thoughts (Database)

- Atlassian Linux Shell Scripting the confluence remote backup methods (Linux)

- linux remote control software (Linux)

- awk variables (Linux)

- STL in the list of erase () method (Programming)

- XtraBackup achieve non-stop use of master-slave synchronization service (Database)

- CentOS build JDK environment (Linux)

- Ubuntu iptables prevent IP attacks (Linux)

- PHP 5.3 New Features Detail (Linux)

- Linux / proc directory Comments (Linux)

- How to make GRub instead of the default Ubuntu software center (Linux)

- JavaScript subarray Deduplication (Programming)

- How to achieve the MySQL Oracle table associated with the two operations update (Database)

- The default permissions for files and directories under Linux computing (Linux)

- Linux NIC configuration (Linux)

- Linux System Getting Started Learning: the curl set custom HTTP header (Linux)

- Seven Steps to Help Google Chrome Speed - (Linux)

- To setup Cocos2dx project under Ubuntu (Linux)

- Use ISO document production OpenStack used CoreOS mirror (Linux)

- Java polymorphism and exception handling (Programming)

  Shell array: Define Shell array, the array length
  Add Date : 2018-11-21      
  Shell in programming a lot stronger than Windows batch, whether it is in circulation, operations.

bash supports one-dimensional array (does not support multidimensional arrays), and does not define the size of the array. Similar to the C language, the subscript of array elements from 0 start number. Gets an array of elements to take advantage of the subscript, the subscript may be an integer or arithmetic expression whose value should be greater than or equal to 0.
Define arrays

In the Shell, use parentheses to denote an array, the array elements with the "space" symbols separated. The general form of an array is defined as follows:

 array_name = (value1 ... valuen)


array_name = (value0 value1 value2 value3)


array_name = (

You can also define separately for each component of the array:

array_name [0] = value0
array_name [1] = value1
array_name [2] = value2

You can not use a continuous index, and there is no limit under the target range.
Read array

The general format is read array element value:
    $ {Array_name [index]}

valuen = $ {array_name [2]}

for example:

#! / Bin / sh
NAME [0] = "Zara"
NAME [1] = "Qadir"
NAME [2] = "Mahnaz"
NAME [3] = "Ayan"
NAME [4] = "Daisy"
echo "First Index: $ {NAME [0]}"
echo "Second Index: $ {NAME [1]}"

Run the script, output:

$. / Test.sh
First Index: Zara
Second Index: Qadir

Use the @ or * can get an array of all the elements, such as:

$ {Array_name [*]}
$ {Array_name [@]}

for example:

#! / Bin / sh
NAME [0] = "Zara"
NAME [1] = "Qadir"
NAME [2] = "Mahnaz"
NAME [3] = "Ayan"
NAME [4] = "Daisy"
echo "First Method: $ {NAME [*]}"
echo "Second Method: $ {NAME [@]}"

Run the script, output:

$. / Test.sh
First Method: Zara Qadir Mahnaz Ayan Daisy
Second Method: Zara Qadir Mahnaz Ayan Daisy

Get length of the array

Gets an array of lengths of string length and get the same way, for example:
Copy plaintext new window

# To obtain the number of array elements

length = $ {# array_name [@]}

Or #

length = $ {# array_name [*]}

# Obtain a single element of the array length

lengthn = $ {# array_name [n]}
- CentOS 6.6 install Oracle 11gR2 database (Database)
- Oracle can not change the tablespace to backup mode in non-archive mode (Database)
- Ubuntu file security removal tool (Linux)
- Nginx log cutting and MySQL script regular backup script (Server)
- Ubuntu installed Gimp 2.6.11 (stable version) with PPA (Linux)
- MySQL partition table Comments (Database)
- using the ssh command to check the socket / Network Connections (Linux)
- Tmux create the perfect terminal management tool under CentOS (Linux)
- Nodejs command-line program development tutorial (Programming)
- System Security: Build Linux with LIDS steel castle (Linux)
- Eclipse installs support for Java 8 (Linux)
- Linux security settings Notes (Linux)
- Grading defense against Linux server attacks (Linux)
- 10 useful Linux command line tips (Linux)
- Android and OpenCV2.4.4 build an image processing framework (2013 edition) (Linux)
- Denyhosts prevent hackers using SSH scanning (Linux)
- Linux stand-alone OGG synchronous Oracle 11g DB test (Database)
- Redis Design and Implementation study notes (Database)
- Linux delete duplicate files Artifact: dupeGuru (Linux)
- Linux alpine use the command line to access Gmail (Linux)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.