Understanding the concept of "DRY," which stands for "don't repeat yourself," is crucial for efficient Bash scripting. Instead of repeating commands within scripts, functions can be employed to enhance code reusability. For instance, a simple function can be defined as foo() { echo "Hello, World!" }, allowing it to be called multiple times, thereby streamlining the scripting process.
Functions can also be more complex. A practical example is a backup function defined as backup() { cp "foo.txt" "foo.txt.backup-$(date +%Y%m%d)" }, which creates a dated backup of a specified file. This demonstrates how functions can encapsulate commands for repeated use, making scripts more organized and maintainable.
Passing arguments to functions further enhances their functionality. In a function like foo() { echo "${1}" "${2}" }, the placeholders ${1} and ${2} represent the first and second arguments, respectively. This allows users to customize function calls, as shown in examples like foo "Hello, " "World!" and foo "Hello, " "Universe!"