1

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);
2
  • 1
    you are not quoting the second $file reference. In fact, you are missing a lot of quotes, e.g. in the function's echo statement and around the $(…) subshell in the last line. This makes all these strings subject to multiple shell expansions. Commented Oct 1, 2019 at 15:37
  • 1
    Can you provide sample input that fail, expected output, and actual output ? Commented Oct 1, 2019 at 15:39

1 Answer 1

1

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}")"

Sign up to request clarification or add additional context in comments.

1 Comment

You should quote the echo argument and the subshell in the last line, i.e. echo "${fileName//[\"\'\ ]/}" and "${OUTPUT_PATH}$( cleanUpName "${file}")"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.