You can create an array in a shell script by explicitly declaring an array or by setting values at specific indices.
Example 1: Explicit Declaration
# Declare an array with explicit elementsmy_array=("apple" "banana" "cherry")
Example 2: Using Indices
# Declare an array and set values at specific indicesmy_array[0]="apple"my_array[1]="banana"my_array[2]="cherry"
Accessing Array Elements
You can access elements of an array using the index of the element enclosed in curly braces.
# Access the first elementecho "${my_array[0]}" # Outputs 'apple'# Access all elementsecho "${my_array[@]}" # Outputs 'apple banana cherry'
Passing Arrays to Functions
To pass an array to a function in Bash, you need to pass it as a series of arguments expanded at the time of call.
Example Function Definition
# Function to print all elements of an arrayprint_array() { echo "Array elements:" for element in "$@"; do # Iterate over all arguments echo "$element" done}
Calling the Function with Array
# Create an arrayfruits=("apple" "banana" "cherry")# Pass the array to functionprint_array "${fruits[@]}"
Modifying Arrays
You can modify arrays by directly setting values at specific indices or by appending new elements.
# Modify an elementmy_array[1]="blueberry"# Append an elementmy_array+=("date")
Summary
Arrays in shell scripting are versatile and can be used for managing lists of items. By understanding how to create, modify, and pass arrays to functions, you can effectively manage complex data structures within your scripts.
Process each element of the array CODE_EXTENSIONS_ARRAY separately in a pipeline (pipe)
Wrapper way
CODE_EXTENSIONS_ARRAY=( "kt" "kts" "java" "js" "ts" "html" "css" "scss" "xml" "yml" "yaml" "sh" "py")arrays.print_elements() { local -n array_ref="$1" # Use a nameref to refer to the input array printf '%s\n' "${array_ref[@]}"}main() { arrays.print_elements CODE_EXTENSIONS_ARRAY | while IFS= read -r element; do echo "Processing: $element" done}main "${@}" || exit 1
Raw
Example: Processing Each Array Element in a Pipeline
# Export the arrayexport CODE_EXTENSIONS_ARRAY=()CODE_EXTENSIONS_ARRAY+=("kt")CODE_EXTENSIONS_ARRAY+=("kts")CODE_EXTENSIONS_ARRAY+=("java")CODE_EXTENSIONS_ARRAY+=("js")CODE_EXTENSIONS_ARRAY+=("ts")CODE_EXTENSIONS_ARRAY+=("html")CODE_EXTENSIONS_ARRAY+=("css")CODE_EXTENSIONS_ARRAY+=("scss")CODE_EXTENSIONS_ARRAY+=("xml")CODE_EXTENSIONS_ARRAY+=("yml")CODE_EXTENSIONS_ARRAY+=("yaml")CODE_EXTENSIONS_ARRAY+=("sh")CODE_EXTENSIONS_ARRAY+=("py")# Process each element in the arrayprintf '%s\n' "${CODE_EXTENSIONS_ARRAY[@]}" | while IFS= read -r extension; do # Process each extension (replace `echo` with your processing command) echo "Processing: $extension"done
Explanation:
printf '%s\n' "${CODE_EXTENSIONS_ARRAY[@]}":
Outputs each element of the array on a separate line.
| while IFS= read -r extension; do:
Pipes the output of printf to a while loop, where each line is read into the variable extension.
Processing Logic:
Replace echo "Processing: $extension" with the actual command or logic to process each element.
Advanced Example: Combining with a Command
If you want to pass each array element as an argument to a command, you can use xargs: