Philou
Philou

Reputation: 107

Bash function not returning expected output

I'm trying to write a function for my Bash script to keep it DRY but for some reason the output of the code is not the same as when it's not inside a function.

What am I missing ?

Working:

#Get file name from file path
fileName="$(basename "$file")";
#Remove " ' and white space from name
fileName=${fileName//[\"\'\ ]/};
convert "$file" -resize $RESOLUTION\> "$OUTPUT_PATH"$fileName;

Not working:

function cleanUpName() {
  #Get file name from file path
  fileName="$(basename "$1")";
  #Remove " ' and white space from name
  echo ${fileName//[\"\'\ ]/};
}

convert "$file" -resize $RESOLUTION\> "$OUTPUT_PATH"$( cleanUpName $file);

Upvotes: 1

Views: 64

Answers (1)

Philou
Philou

Reputation: 107

As @Robin479 suggested in the comments, I was missing quotes for my file variable working code is as follow:

function cleanUpName() {
  #Get file name from file path
  fileName="$(basename "$1")";
  #Remove " ' and white space from name
  echo "${fileName//[\"\'\ ]/}"
}
convert "$file" -resize $RESOLUTION\> "$OUTPUT_PATH$( cleanUpName "${file}")"

Upvotes: 1

Related Questions