Reputation: 55
I am trying to pass a to fill an array with a list of directories using a simple shell script. However when I pass /bin/*
into the program as the first parameter $1
turn to /bin/bash
(the first directory in /bin). My code is below for reference. Can I not pass *
in a parameter?
#!/bin/bash
declare -a d
placeholder=0
for filename in ${1}; do
d[${placeholder}]=${filename:5}
((placeholder++))
done
echo "$1"
The result of the echo is /bin/bash
Thanks!
Upvotes: 0
Views: 37
Reputation: 295687
A glob is replaced by the shell with a list of names it matches.
Take advantage of this, and read the list of names passed on your argument list:
#!/bin/bash
declare -a d
placeholder=0
for filename in "$@"; do # or just ''for filename; do''
d[${placeholder}]=${filename:5}
((placeholder++))
done
# print your array
declare -p d
# or, to print your array's contents one entry per line:
printf '%s\n' "${d[@]}"
...or, far easier:
#!/bin/bash
d=( "${@##*/}" ) # assuming the :5 is intended to leave only filenames
declare -p d
Upvotes: 1
Reputation: 29266
*
is being expanded by the shell before it calls your script. Try calling as yourscript '/bin/*'
(note the quotes).
Upvotes: 0