Arrays
Part of the Bash course
So far, one variable has held one value. name="Ada" holds one string. If you wanted to hold three names, you would either need three separate variables, name1, name2, name3, or you would stuff them into one string separated by spaces and hope none of them ever contains a space of its own. Neither is a good plan once a list has more than a couple of items.
Picture an egg carton. Each cup in the carton holds one egg, and each cup has a fixed position: the first cup, the second cup, and so on. You do not need a separate variable for "egg in cup one" and "egg in cup two", you just have one carton and you refer to a slot by its position. An indexed array in Bash is exactly this: one variable, many slots, each slot numbered starting from 0. ...
Want to learn Bash Arrays in a real environment?
Click below, get a fresh Linux box with Bash ready to go, and work through Arrays hands-on with an AI tutor that knows your code.
Start this lessonArraysWhat you'll learn
- Indexed arrays
- ${arr[@]}
- ${#arr[@]} length
- Associative arrays declare -A
- Iterating arrays
Read this lesson in your language
All lessons in this course
12 lessons- 1What Is Bash20 min
- 2Shebang and Your First Script25 min
- 3Variables and Quoting25 min
- 4Conditionals and Tests30 min
- 5Loops25 min
- 6Functions25 min
- 7Arrays25 min
- 8String and Arithmetic Expansion25 min
- 9IO Redirection and Pipes25 min
- 10Error Handling and Traps30 min
- 11Debugging and Best Practices25 min
- 12Capstone: Server Bootstrap Script60 min
