2

From Glenn's reply:

Read your execve(2) man page. The limitation on a single optional argument is OS dependent. Linux treats all words after the interpreter as one single argument

If you want to do this:

#! /path/to/interpreter arg1 arg2 arg3

You should in fact do this

#!/bin/sh
exec /path/to/interpreter arg1 arg2 arg3 "$@"

If I want to write a script which contains some code in the script language,:

#! /path/to/interpreter arg1 arg2 arg3

<script content in the script language>

according to Glenn's reply, I should instead write the script as:

#!/bin/sh
exec /path/to/interpreter arg1 arg2 arg3 "$@"

Then where should I write <script content in the script langauge>? Since it is written in the script language but not in the shell language, so it can't be placed in the the shell script, correct? Where should I place it?

Thanks.

Tim
  • 101,790

2 Answers2

3

In addition to the options that muru gave, there is another option. Depending on the complexity and length of the script, you can put the script inside the shell script (whether it's generally useful is another question!) Using the "here-document" mechanism, you can have the shell pipe embedded text into another program, like this:

test.sh

#!/bin/sh
exec /usr/bin/python3 <<EOF
import datetime
print("The time is now "+str(datetime.datetime.now()))
EOF

I wouldn't use this if the program is very large, because it's harder to edit these kinds of scripts; they're embedded in another language, so you have to extract the script to do any modifications if you want to test them.

As an oddity, note that the shell still expands shell variables and interprets special characters using this method:

#!/bin/sh
exec /usr/bin/python3 <<EOF
print("The current user is $USER and date is $(date).")
EOF

Returns The user is erik and date is Fri Apr 6 22:14:23 MDT 2018.

ErikF
  • 4,042
  • Only if the token after << is (wholly) unquoted. This is explained in the wikipedia page you linked, or e.g. https://www.gnu.org/software/bash/manual/html_node/Redirections.html#Here-Documents – dave_thompson_085 Apr 07 '18 at 04:26
  • Also, the script effectively loses stdin... – muru Apr 07 '18 at 04:31
  • There are plenty of gotchas with this method! As a quick-and-dirty solution it's OK, but there are definitely better ideas. – ErikF Apr 07 '18 at 04:41
2

You'll have to trick the interpretor into ignoring the exec line, and put the script contents normally in the file. Options include:

  • Polyglot scripts, where the exec command is written so that it looks like comment or a string or similar to the actual interpretor. Examples:
  • feed the script to the interpretor with the initial lines stripped off
muru
  • 72,889