Split csv file using Automator Service (Finder Context Menu)
I'd write the code a bit differently, and here is an example of how I'd do it:

#!/bin/bash

for f in "$@"; do
    if [[ -f $f ]]; then
        d="$(dirname "$f")"
        n="$(basename "$f")"
        t='/tmp'
        if [[ ${n##*.} =~ [cC][sS][vV] ]]; then
            head -1 "$f" &gt; $t/h.tmp
            tail -n +2 "$f" | split -a 3 -l 50 - $t/tmp.
            i=1
            for s in $t/tmp.a??; do
                fn="$d/${n%.*}.$(printf '%03d' $i).csv"
                if [[ ! -f $fn ]]; then 
                    cat $t/h.tmp $s &gt; "$fn"
                    ((i++))
                else
                    rm $t/h.tmp $t/tmp.a??
                    echo "The file '"$fn"' already exists!"
                    exit
                fi
            done
            rm $t/h.tmp $t/tmp.a??
            echo ''
        fi
    fi
done



As presently coded, it handles one or more files passed to the service.
Makes sure the object being acted upon is a file, not a directory.
Makes sure the file has a .csv extension (regardless of the case of the extension).
Creates the temporary files in: /tmp
Checks to see that the output filename doesn't already exist, and if it does, it cleans up and exits.
Writes to a file with a numerically incremented filename, e.g. file.001.csv, file.002.csv, etc., in the same directory as the file(s) passed to the service.
Removes the temporary files created in: /tmp 
As presently coded, it handles files with a line count of up to 49,950 split to 50 line files, not counting the header.


Note that no error handling is coded for the total line count of the source file, however, could be easily added.
Or easily modified to handle files with a line count of up to 499,950 split to 50 line files, not counting the header, by changing -a 3 of the split command to -a 4 and '%03d' of the printf command to '%04d'. You'd also change $t/tmp.a?? in  for s in $t/tmp.a??; do and rm $t/h.tmp $t/tmp.a?? to: $t/tmp.a???



I'd also add a Run Apple Script action to the service, with the following code:

on run {input, parameters}
    if (item 1 of input) is "" then
        display notification "Splitting of the target file(s) is finished!" with title "Split CSV File(s)"
    else
        display notification (item 1 of input as string) with title "Split CSV File(s)"
    end if
end run


This enables the output of the echo commands in the Run Shell Script action to display a notification if a output file already exists or when the splitting is finished.

Note that while notification could be done from within the Run Shell Script action using osascript, nonetheless, I did it this way as it was easier to code.





This was tested on a file named file.csv in Finder, which has 200 lines in it, and the images below show what was created by the Run Shell Script action portion of the Automator service when run on the file.