title: How do I use Bash to create a copy of a file with an extra suffix before the extension?
body: <p>This title is a little confusing, so let me break it down. Basically I have a full directory of files with various names and extensions:</p>
  
  <pre><code>MainDirectory/
      image_1.png
      foobar.jpeg
      myFile.txt
  </code></pre>
  
  <p>For an iPad app, I need to create copies of these with the suffix <em>@2X</em> appended to the end of all of these file names, before the extension - so I would end up with this:</p>
  
  <pre><code>MainDirectory/
      image_1.png
      image_1@2X.png
      foobar.jpeg
      foobar@2X.jpeg
      myFile.txt
      myFile@2X.txt
  </code></pre>
  
  <p>Instead of changing the file names one at a time by hand, I want to create a script to take care of it for me. I currently have the following, but it does not work as expected:</p>
  
  <pre><code>#!/bin/bash
  
  FILE_DIR=.
  
  #if there is an argument, use that as the files directory. Otherwise, use .
  if [ $# -eq 1 ]
  then
    $FILE_DIR=$1
  fi
  
  for f in $FILE_DIR/*
  do
    echo "Processing $f"
    filename=$(basename "$fullfile")
    extension="${filename##*.}"
    filename="${filename%.*}"
    newFileName=$(echo -n $filename; echo -n -@2X; echo -n $extension)
    echo Creating $newFileName
    cp $f newFileName
  done
  
  exit 0
  </code></pre>
  
  <p>I also want to keep this to pure bash, and not rely on os-specific calls. What am I doing wrong? What can I change or what code will work, in order to do what I need?</p>