ZhangZhihui's Blog  

 

zzh@ZZHPC:~/aaa$ lh
-rw-rw-r--  1 zzh zzh    0 Jun 19 15:23 a.PNG
-rw-rw-r--  1 zzh zzh    0 Jun 19 15:23 b.PNG
-rw-rw-r--  1 zzh zzh    0 Jun 19 15:33 a.png

zzh@ZZHPC:~/aaa$ for file in *.*; do
>                    ext="${file##*.}"
>                    base="${file%.*}"
>                    if [[ "$ext" =~ [A-Z] ]]; then
>                        newfile="${base}.${ext,,}"
>                        mv "$file" "$newfile"
>                    fi
>                done

zzh@ZZHPC:~/aaa$ lh
-rw-rw-r--  1 zzh zzh    0 Jun 19 15:23 a.png
-rw-rw-r--  1 zzh zzh    0 Jun 19 15:23 b.png

 

The ${ext,,} syntax is a parameter expansion feature in Bash (version 4.0 and above) that converts a string (or variable) to lowercase.

Explanation

  • ${variable,,} is a case modification operator in Bash.

  • It means: expand the variable, and convert all uppercase letters in the value to lowercase.

 

Related Variants

  • ${ext,,} → lowercase entire string

  • ${ext,,[A-Z]} → lowercase only specific letters

  • ${ext^^} → uppercase entire string

  • ${ext~} → toggle case (first character only)

Notes

  • This feature is only available in Bash 4.0+.

  • It's a Bash-specific extension, so not all POSIX-compliant shells (like sh) support it.

 

posted on 2025-06-19 15:52  ZhangZhihuiAAA  阅读(7)  评论(0)    收藏  举报