Shebang line limit in bash and linux kernel

独自空忆成欢 提交于 2019-11-27 04:58:53
Ignacio Vazquez-Abrams

Limited to 127 chars on 99.9% of systems due to kernel compile time buffer limit.

It's limited in the kernel by BINPRM_BUF_SIZE, set in include/linux/binfmts.h.

If you don't want to recompile your kernel to get longer shebang lines, you could write a wrapper:

#!/bin/bash

if [[ $# -eq 0 ]]; then
    echo "usage: ${0##*/} script [args ...]"
    exit
fi

# we're going to expand a variable *unquoted* to use word splitting, but
# we don't want to have path expansion effects, so turn that off
set -f

shebang=$(head -1 "$1")
if [[ $shebang == '#!'* ]]; then
    interp=( ${shebang#\#!} )        # use an array in case a argument is there too
else
    interp=( /bin/sh )
fi

# now run it
exec "${interp[@]}" "$@"

and then run the script like: wrapper.sh script.sh

Updated @glenn jackman's script to support passing in command line arguments.

Incidentally, I ran into this problem when creating a python virtualenv inside of a very deep directory hierarchy.

In my case, this was a virtualenv created inside a Mesos framework dir.

The extra long shebang rendered calling xxx/.../venv/bin/pip useless.

The wrapper script proved most useful.

#!/usr/bin/env bash

script="$1" 
shebang=$(head -1 "$script")

# use an array in case a argument is there too
interp=( ${shebang#\#!} )        

# now run it, passing in the remaining command line arguments
shift 1
exec "${interp[@]}" "$script" "${@}"
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!