简体   繁体   中英

Bash - get basename from loop

I am trying to get basename from loop but this only returns me "*" .

FILES=("/home/aaaa/bbbb/*") #Get all folders
for f in "${FILES[@]}"
do
  basename "$f"
done

What I am doing wrong?

You don't even need to create an array and execute your loop like this:

for f in /home/aaaa/bbbb/*
do
  # get the basename using pure BASH
  base="${f##*/}"
  echo "base is: $base"
done

By using ("/home/aaaa/bbbb/*") , an array is created with just one literal string "/home/aaaa/bbbb/*" .

Instead, get rid of the quotes and simply say:

FILES=(/home/aaa/bbb/*)
for f in "${FILES[@]}"
do
  basename "$f"
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM