Issue
I'm trying to get a list of my top-level dirs in a subdir, so I can post process them, e.g., delete certain ones. I have
# List the top-level dirs and create an array with the resul
DIRS=`ls -1`
IFS=$'\n' read -ra TOP_DIRS <<< "$DIRS"
# Iterate the array
for D in "${TOP_DIRS[@]}"; do
# For now, just echo the dirs
echo $D
done
The ls -1
command gives me this for example
00 PRM - AUTO GA
00 PRM - AUTO GA Prod
00 PRM - AUTO GA [email protected]
00 PRM - AUTO GA [email protected]@tmp
00 PRM - AUTO GA STG
00 PRM - AUTO GA [email protected]
00 PRM - AUTO GA [email protected]@tmp
However, the for
loop only echoes the first value, that is
$ ./clean_workspace.sh
00 PRM - AUTO GA
So obviously my IFS
statement is wrong. What am I missing? TIA!
Solution
There's no need for ls
. Just use a wildcard in an array literal.
top_dirs=(*)
BTW, it's bad style to use uppercase names for your variables. These are conventionally reserved for environment variables.
Answered By - Barmar Answer Checked By - Katrina (WPSolving Volunteer)