1) Accessing Elements
[root@oel01db ~]# fruits=("Apple" "Banana" "Cherry")
[root@oel01db ~]#
[root@oel01db ~]# echo "${fruits[@]}"
Apple Banana Cherry
[root@oel01db ~]#
To print each element on a new line:
[root@oel01db ~]# printf '%s\n' "${fruits[@]}"
Apple
Banana
Cherry
[root@oel01db ~]#
How printf "%s\n" "${fruits[@]}" Works
The Template
-
%s→ Placeholder for a string -
\n→ Newline
The Data
${fruits[@]} contains:
-
Apple
-
Banana
-
Cherry
The Execution (Implicit Loop Behavior)
printf automatically loops over all arguments:
-
Apple → inserted into
%s→ newline -
Banana → inserted into
%s→ newline -
Cherry → inserted into
%s→ newline
This is called the implicit loop of printf.
Common Format Specifiers
| Specifier | Data Type | Example | Output |
|---|---|---|---|
%s | String | printf "%s" "Hello" | Hello |
%d | Integer | printf "%d" 10 | 10 |
%f | Float | printf "%f" 1.5 | 1.500000 |
%q | Quoted String | printf "%q" "Space Word" | Space\ Word |
2) Counting Elements and Lengths
Number of Elements
[root@oel01db ~]# echo ${#fruits[@]}
3
[root@oel01db ~]#
${#array[@]} → Returns total number of elements.
Length of a Specific Element
[root@oel01db ~]# echo ${#fruits[1]}
6
[root@oel01db ~]#
Index 1 = "Banana" → length = 6.
${#array[index]} → Returns string length of that element.
3) Array Slicing
Syntax:
${array[@]:start:length}
Length is optional. If omitted → Bash returns everything from start to end.
Print Full Array
[root@oel01db ~]# printf '%s\n' "${fruits[@]}"
Apple
Banana
Cherry
[root@oel01db ~]#
From Index 1 to End
[root@oel01db ~]# printf '%s\n' "${fruits[@]:1}"
Banana
Cherry
[root@oel01db ~]#
Explanation
-
Index 0 → Apple (Skipped)
-
Index 1 → Banana (Included)
-
Index 2 → Cherry (Included)
Exactly One Element from Index 1
[root@oel01db ~]# printf '%s\n' "${fruits[@]:1:1}"
Banana
[root@oel01db ~]#
First Two Elements
[root@oel01db ~]# printf '%s\n' "${fruits[@]:0:2}"
Apple
Banana
[root@oel01db ~]#
Last Element
[root@oel01db ~]# printf '%s\n' "${fruits[@]: -1}"
Cherry
[root@oel01db ~]#
Note: There must be a space before
-1.
4) Search and Replace in Arrays
Syntax:
${array[@]/search/replace}
Example:
[root@oel01db ~]# apps=("vscode" "vlc" "vim")
[root@oel01db ~]#
[root@oel01db ~]# echo ${apps[@]/v/X}
Xscode Xlc Xim
[root@oel01db ~]#
Using printf:
[root@oel01db ~]# printf '%s\n' "${apps[@]/v/X}"
Xscode
Xlc
Xim
[root@oel01db ~]#
This replaces the first occurrence of v in every element.
5) Cutting Characters (Pattern Removal)
-
#→ Remove from front -
%→ Remove from back
Remove from End
[root@oel01db ~]# printf '%s\n' "${fruits[@]%x}"
Apple
Banana
Cherry
[root@oel01db ~]#
Remove e from end:
[root@oel01db ~]# printf '%s\n' "${fruits[@]%e}"
Appl
Banana
Cherry
[root@oel01db ~]#
Remove from Beginning
[root@oel01db ~]# printf '%s\n' "${fruits[@]#A}"
pple
Banana
Cherry
[root@oel01db ~]#
6) Getting Indexes and Keys
${fruits[@]} → Values
${!fruits[@]} → Index numbers (or keys)
Indexed Array Example
[root@oel01db ~]# fruits=(Apple Banana Cherry)
[root@oel01db ~]#
[root@oel01db ~]# printf 'Index: %s\n' "${!fruits[@]}"
Index: 0
Index: 1
Index: 2
[root@oel01db ~]#
Associative Arrays (Key-Value Pairs)
[root@oel01db ~]# unset colors
[root@oel01db ~]#
[root@oel01db ~]# declare -A colors
[root@oel01db ~]# colors[apple]="red"
[root@oel01db ~]# colors[banana]="yellow"
[root@oel01db ~]# colors[cherry]="dark red"
[root@oel01db ~]#
[root@oel01db ~]# printf 'Value: %s\n' "${colors[@]}"
Value: dark red
Value: red
Value: yellow
[root@oel01db ~]#
[root@oel01db ~]# printf 'Key: %s\n' "${!colors[@]}"
Key: cherry
Key: apple
Key: banana
[root@oel01db ~]#
In associative arrays:
-
${colors[@]}→ Values -
${!colors[@]}→ Keys
No comments:
Post a Comment