I'm fairly new to working with arrays in bash scripting. Basically I have an array created, for example:
array=( /home/usr/apple/ /home/usr/pineapple/ /home/usr/orange/ )
I want to modify this array so that the resulted modified array will look like:
array=( apple/ pineapple/ orange/ )
I tried with this block of code:
basepath=/home/usr/
# modify each item in array to remove leading /home/usr
for i in "${array[@]}"
do
array[$i]=${i#$basepath}
done
# print all elements from array
for i in ${array[@]}
do
printf "%s\n" "$i"
done
This gave me an syntax error: operand expected (error token is /home/usr/apple)
Expected result is it will print out the new modified array of just apple/, pineapple/ and orange/