Print a "shebang" line, or prepend it to a file
Is the code readable? Can this code be simpler?


Basically readable, although I prefer early returns and things like that.

Ported to bash but testing limited:

# shebang [-iv] [-t interpreter] [-I extension] [-J [extension]] [filename]
#
# If no filename is given, print a shebang. If a filename is given, append a
# shebang to the file and print its contents. If a filename is given and the -I
# or -J options are specified, append a shebang to the file in place.
#
# OPTIONS
#   -i
#    Interactive mode; ask for confirmation first.
#   -I
#    Modify "filename" in place (as in `sed -i`) using specified extension.
#   Extension is mandatory.
#   -J
#    Same as -I, but extension is mandatory only if the string "gsed" cannot
#    be found in the output of `which sed`. Overridden by -I.
#   -t
#    Specify an interpreter, e.g. `shebang -t zsh` produces "#!/usr/bin/env zsh"
#   -v 
#    Print (to stderr) the interpreter being used.

shebang () {
    local inplace inplace2 verbose interactive
    local interpreter input_shebang shebang continue
    local sed_command gsed_avail _nextopt _line _readbool
    local OPT OPTIND OPTARG
    # HACK: shift manually when encounters J
    while getopts 't:I:viJ' OPT; do case "$OPT" in
        i)  interactive=1;;
        I)  inplace="$OPTARG";;
        J)  eval "nextopt=\$$OPTIND";
            case "$_nextopt" in
                -*) inplace2='-J';;
                *)  inplace2="-J $_nextopt"; shift;;
            esac;;
        t)  interpreter="$OPTARG";;
        v)  verbose=1;;
    esac; done
    shift $((OPTIND-1))

    (( $? )) &amp;&amp; return 1

    if [[ -n $1 ]]; then
        IFS='' read -r _line &lt; "$1"
        if (( $? )); then
            echo "Unable to read $1." &gt;&amp;2
            return 1
        fi
        # if echo "$_line" | grep ^#!; then
        if [[ "$_line" =~ ^\#! ]]; then
            echo "$1 already has a shebang." &gt;&amp;2
            return 1
        fi
    fi

    if [[ (-z "$interpreter") &amp;&amp; (-z "$1") ]]; then
          echo "The -t option is mandatory if no argument is supplied." &gt;&amp;2
          return 1
    fi

    if [[ (-z "$interpreter") &amp;&amp; (-n "$1") ]]; then
          interpreter=$(filename=$(basename "$1"); [[ "$filename" = *.* ]] &amp;&amp; echo "${filename##*.}")  # grab extension
          interpreter=${interpreter/#* *}  # "extensions" with whitespace probably aren't legit
          : ${interpreter:=sh}  # assume sh if no extension and no -t option
    fi

    if [[ "$ZSH_VERION" ]]; then
        # reads one character.
        _sb_read1(){ read -k 1 "$@"; }
    else
        # ksh/bash
        _sb_read1(){ read -n 1 "$@"; }
    fi

    shebang="#!/usr/bin/env $interpreter"

    if [[ -n $verbose ]]; then
        echo "Using interpreter '$interpreter'" &gt;&amp;2
    fi

    if [[ -n $interactive ]]; then
        while true; do
            printf "continue? Shebang will be '$shebang'. Ok? y/n: "
            # ah, zsh/bash have different ideas on read.
            _sb_read1 _readbool
            echo
            case "$_readbool" in
                [nN])   return 1;;
                [yY])   break;;
            esac
        done
    fi

    if [[ -z "$1" ]]; then
          echo $shebang
          return 0
    fi

    if type gsed; then
        sed_command="1 i\\$shebang\n"
        if [[ -n $inplace ]]; then
            gsed "${inplace/-I/-i}" "$sed_command" "$1"
        elif [[ -n $inplace2 ]]; then
            gsed "${inplace2/-J/-i}" "$sed_command" "$1"
        else
            gsed "$sed_command" "$1"
        fi
    else
        sed_command=$'1i\n'$shebang'\n'
        if [[ -n $inplace ]]; then
            sed "${inplace/-I/-i}" "$sed_command" "$1"
        elif [[ (-n $inplace2) &amp;&amp; (-z ${inplace2#-J}) ]]; then
            echo "'-J' was given without an argument, but 'gsed' is unavailable. Specify an explicit extension or use -I."
        elif [[ -n ${inplace2#-J} ]]; then
            sed "${inplace2/-J/-i}" "$sed_command" "$1"
        else
            sed "$sed_command" "$1"
        fi
    fi
}


Using that grep for if gives extra ksh compatibility (untested).