Ukufika kukaBash Ngemizekelo

Uluhlu yingqokelela yezinto. KwiBhash, uluhlu olunokuba nomxube wezinto zeentlobo ezahlukeneyo, o.k.t. Imitya kunye neNombolo.

Kule tutorial, sixoxa ngendlela yokwenza kunye nokusebenzisa uluhlu kwiBhash. Siza kugubungela imisebenzi emininzi efana nokutshixa, ukushicilela, ukufumana ubungakanani kunye nokuguqula imixholo.



Uyenza njani iArrows kwiBhash

Zimbini iindlela esinokwenza ngazo uluhlu lweBhash:


Sebenzisa igama eliyintloko lokubhengeza

declare -a my_bash_array

oku kuyakwenza uluhlu olunoluhlu olunegama 'my_bash_array'.

Ukuqalisa uluhlu

Sinokwenza kwaye siqalise uluhlu lokuhamba kubhabho sisebenzisa isabelo somsebenzi = kunye nezinto ezingaphakathi kubrashi obugobileyo ():


my_bash_array=('apple' 'orange' 'banana')

Okanye, sinokuchaza ngokucacileyo isalathiso



my_bash_array[0]='apple' my_bash_array[1]='orange' my_bash_array[2]='banana' Phawula:Akubikho zithuba macala omabini = umqhubi.

Ubude beBash Array

Ukufumana ubude okanye ubungakanani boluhlu, sisebenzisa ${#array_name[@]}.

Umzekelo:

my_bash_array=(foo bar baz) echo 'the array contains ${#my_bash_array[@]} elements' #Output the array contains 3 elements

IBhash Array Loop

Ukulungisa zonke izinto ezikuluhlu lweBash, sinokusebenzisa ilogo:


#!/bin/bash my_array=(foo bar baz) # for loop that iterates over each element for i in '${my_array[@]}' do
echo $i done

Iziphumo:

foo bar baz

Ukuprinta zonke izinto

Ukuprinta zonke izinto zoluhlu ngaphandle kweluphu, singasebenzisa le syntax elandelayo:

echo ${my_array[@]}

Yongeza izinto kuluhlu

Ukongeza izinto kuludwe sisebenzisa += umqhubi. Oku kuyongeza into ukuya esiphelweni kuluhlu.

Umzekelo:


my_array=(foo bar) my_array+=(baz) echo '${my_array[@]}' foo bar baz

Okanye sinokusebenzisa isalathiso ukongeza into:

my_array=(foo bar) my_array[2]=baz echo '${my_array[@]}' foo bar baz

Cima izinto kwiArray

Ukucima into kuluhlu lweBash, sisebenzisa unset umyalelo.

Umzekelo:

my_array=(foo bar baz) unset my_array[1] echo ${my_array[@]} foo baz

Isiphelo

Kolu qeqesho sigubungele uluhlu lweBash; Uyenza njani kwaye uyiqale njani i-arrash eBhash kunye nendlela yokufumana ubude, ukujikeleza izinto, ukuprinta izinto kunye nokuguqula imixholo yoluhlu.