I am writing a bash script to be run on macOS Sonoma
$bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin23)
Copyright (C) 2007 Free Software Foundation, Inc.
My script is to read a source file line by line, apply some logic to each line, and write it into one of the target files.
#!/bin/bash
if [[ $# -eq 0 ]]; then
echo "Usage: $0 <source file> <target 1> <target 2> ... <target n>"
exit 1
elif [[ $# -eq 1 ]]; then
echo "Usage: $0 <source file> <target 1> <target 2> ... <target n>"
exit 1
fi
numFiles=$(($# - 1))
echo "${numFiles} target files"
sourceFile="$1"
echo "Source file: ${sourceFile}"
targetFiles=(${@: 2})
echo "Target files: ${targetFiles}"
I would expect targetFiles to be an array of the target files.
However, when I run this as
splitFile.sh source.txt t1.txt t2.txt t3.txt
I only get
3 target files
Source file: source.txt
Target files: t1.txt
I would expect the last line to be
Target files: t1.txt t2.txt t3.txt
How do I fix this?
targetFilesis an array. To print the array, you need to use array notationwill give you all values.