Bash Arrays, Strings, and Text Handling

Indexed and associative arrays, string handling, read/mapfile, and common Bash text-processing patterns.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Indexed Arrays

Create, inspect, and modify indexed arrays.

Create indexed array

Create an indexed array.

bashANYarraysindexed
bash
files=(a.txt b.txt c.txt)

Whitespace separates elements unless quoted.

Access array element

Read an array element by index.

bashANYarraysindexed
bash
echo "${files[1]}"

Array indexes are zero-based.

Expand all array items

Expand all array items safely.

bashANYarraysexpansion
bash
printf '%s
' "${files[@]}"

Use "${array[@]}" to preserve element boundaries.

Array length

Count array elements.

bashANYarrayslength
bash
echo "${#files[@]}"

Very common in loops and validation.

Append array element

Append one or more elements to an array.

bashANYarraysappend
bash
files+=(d.txt)

Works cleanly for indexed arrays.

Loop through array

Iterate through array elements safely.

bashANYarraysloops
bash
for f in "${files[@]}"; do echo "$f"; done

Always quote "${array[@]}" in loops.

Unset array element

Remove one indexed element.

bashANYarraysunset
bash
unset 'files[1]'

Remaining indexes are preserved; arrays can be sparse.

Associative Arrays

Use key/value maps in Bash.

Create associative array

Declare an associative array.

bashANYarraysassociative
bash
declare -A ports=([http]=80 [https]=443)

Requires Bash 4 or newer.

Read associative array item

Read a value by key.

bashANYarraysassociative
bash
echo "${ports[https]}"

Associative arrays are ideal for lightweight maps.

List associative array keys

Expand associative array keys.

bashANYarrayskeys
bash
printf '%s
' "${!ports[@]}"

Useful for loops and reporting.

Loop through key/value pairs

Iterate through an associative array.

bashANYarraysassociativeloops
bash
for k in "${!ports[@]}"; do echo "$k=${ports[$k]}"; done

Order is not guaranteed.

String Operations

Case conversion, trimming, splitting, and joins.

Lowercase string

Convert a string to lowercase.

bashANYstringscase
bash
name='Hello World'
echo "${name,,}"

Bash supports case modification operators on parameters.

Uppercase string

Convert a string to uppercase.

bashANYstringscase
bash
name='Hello World'
echo "${name^^}"

Useful for normalization and IDs.

Trim prefix pattern

Strip a matching prefix from a string.

bashANYstringsprefix
bash
url='https://example.com'
echo "${url#https://}"

Pattern-based trimming is faster than spawning external tools.

Trim suffix pattern

Strip a matching suffix from a string.

bashANYstringssuffix
bash
file='archive.tar.gz'
echo "${file%.gz}"

Useful for extensions and temporary suffixes.

Split string into array

Split a delimited string into an array.

bashANYstringssplitarrays
bash
IFS=, read -r -a parts <<< 'red,green,blue'
printf '%s
' "${parts[@]}"

Use IFS carefully because it affects word splitting.

Join array with delimiter

Join array elements using IFS.

bashANYstringsjoinarrays
bash
(IFS=,; echo "${parts[*]}")

The subshell limits IFS changes to one command group.

Reading Input

read, mapfile, and input handling patterns.

Prompt user for input

Read one line from stdin with a prompt.

bashANYreadinput
bash
read -r -p 'Enter name: ' name

-r prevents backslash escaping during input.

Read secret silently

Read secret input without echoing characters.

bashANYreadinputpassword
bash
read -r -s -p 'Password: ' password; echo

Common for credentials in interactive scripts.

Load file into array

Read all lines of a file into an array.

bashANYmapfilearraysfiles
bash
mapfile -t lines < file.txt

mapfile and readarray are Bash builtins.

Read delimited fields

Parse delimited input into multiple variables.

bashANYreadparsing
bash
IFS=: read -r user shell <<< 'root:/bin/bash'
echo "$user $shell"

Useful for colon, comma, or tab-separated data.