Industrial
Industrial

Reputation: 42758

Working with UTF-characters in bash

I am trying to create a script that walks a directory and renames files. I would like to be able to extract the filename and file extension separately, but if the file path contains either spaces or Swedish UTF8-characters such as ÅÄÖ, it breakes.

I've found the below shown snippet to extract the filename + extension here on SO, but as I am seeing that it works on paths with no UTF-chars or whitespace, I am thinking that I am not properly escaping my variables.

Perhaps I am doing something wrong. Any ideas on what I can do to make this work with paths with UTF8 chars and whitespace?

for file in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS; do

  FULLPATH="$file"
  FILENAME=${FULLPATH##*/}
  FILEEXTENSION=${FILENAME##*.}
  BASEDIRECTORY=${FULLPATH%$FILENAME}

  #Log the vars for debugging
  echo "$FULLPATH" >> ~/Desktop/log.txt
  echo "$FILENAME" >> ~/Desktop/log.txt
  echo "$FILEEXTENSION" >> ~/Desktop/log.txt
  echo "$BASEDIRECTORY" >> ~/Desktop/log.txt

done

Upvotes: 1

Views: 3160

Answers (2)

Anya Shenanigans
Anya Shenanigans

Reputation: 94584

The problem is that the NAUTILUS_SCRIPT_SELECTED_FILE_PATH variable is new-line escaped per item.

You need to use:

while read file; do

  FULLPATH="$file"
  FILENAME=${FULLPATH##*/}
  FILEEXTENSION=${FILENAME##*.}
  BASEDIRECTORY=${FULLPATH%$FILENAME}

  #Log the vars for debugging
  echo "$FULLPATH" >> ~/Desktop/log.txt
  echo "$FILENAME" >> ~/Desktop/log.txt
  echo "$FILEEXTENSION" >> ~/Desktop/log.txt
  echo "$BASEDIRECTORY" >> ~/Desktop/log.txt

done <<<"$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"

Upvotes: 2

Mathias Bynens
Mathias Bynens

Reputation: 149484

I have this in my dotfiles:

# Prefer US English and use UTF-8
export LC_ALL="en_US.UTF-8"
export LANG="en_US"

With that configuration, it seems to work:

$ ls -l
total 0
drwxr-xr-x  2 Mathias  staff  68 Jan 17 11:32 test space test
drwxr-xr-x  2 Mathias  staff  68 Jan 17 11:29 test©test
$ for file in *; do echo "$file"; done
test space test
test©test

Upvotes: 0

Related Questions