3

I'm using env in a shell script like so:

#!/usr/bin/env bash
...
...

Mainly so my scripts are more portable. Anyway, i noticed that adding arguments to application that use env doesn't always work...

#!/usr/bin/env bash -x
...
...

yield

/usr/bin/env: ‘bash -x’: No such file or directory
/usr/bin/env: use -[v]S to pass options in shebang lines

I'm unsure on the correct formatting for the arguments. I checked the man page and i didn't see anything that would help either (afaik).

Nordine Lotfi
  • 2,200
  • 12
  • 45
  • 1
    Related: [Multiple arguments in shebang](https://unix.stackexchange.com/questions/399690/multiple-arguments-in-shebang), in particular [this answer](https://unix.stackexchange.com/a/477651/65304) – steeldriver Nov 01 '20 at 13:13
  • Interesting. I didn't found this when i searched on Unix.SE search engine... @steeldriver – Nordine Lotfi Nov 01 '20 at 13:14
  • Yeah, it does...but i already answered it with a similar answer, prior to finding such post(since i didn't found other post that had the solution, like the one you posted). @StephenKitt Should i delete this post, wait 2 days to accept my own answer or accept the linked post you provided? – Nordine Lotfi Nov 01 '20 at 13:20
  • 1
    I’d recommend accepting the dupe. You can still leave your answer if you feel it adds more information. Keeping your question helps make the other question easier to find. – Stephen Kitt Nov 01 '20 at 13:26

1 Answers1

3

Seems using the -S flag on env, like so:

#!/usr/bin/env -S bash -x
...
...

works.

It is interesting to note that, for some reasons, it is not stated in the man page, though, it is shown in the --help output.

Probably has to do with the fact that env only started to have the -S flag when coreutils got updated (to 8.30). I'm guessing most man page aren't updated and still have information from before 8.30 (eg: 8.28 didn't have the -S flag).

reference:

https://jhermann.github.io/blog/linux/know-how/2020/02/28/env_with_arguments.html https://linux.die.net/man/1/env

Nordine Lotfi
  • 2,200
  • 12
  • 45
  • 1
    The man page link is 10 years out of date. – rowboat Nov 01 '20 at 13:15
  • I didn't check other man pages, but most (that are online) aren't kept up to date... @rowboat – Nordine Lotfi Nov 01 '20 at 13:17
  • 2
    Michael Kerrisk’s site is kept up-to-date, that’s one of the better references for manpages — see [`env`](https://man7.org/linux/man-pages/man1/env.1.html). The best reference is usually `man env` on the target system since that will match the installed command. – Stephen Kitt Nov 01 '20 at 13:21
  • yeah, forgot to check man7.org :D. Also, i asked mainly because i sometimes don't have `man` installed (eg: working on embedded systems). @StephenKitt – Nordine Lotfi Nov 01 '20 at 13:22
  • 1
    And for GNU software the best reference is the full manual for the corresponding project; see [the section on `env -S`](https://www.gnu.org/software/coreutils/manual/html_node/env-invocation.html#g_t_002dS_002f_002d_002dsplit_002dstring-usage-in-scripts). – Stephen Kitt Nov 01 '20 at 13:23
  • 1
    Right, embedded systems are another kettle of fish, since they might use other variants of commands (*e.g.* Busybox) which don’t always have the same options. – Stephen Kitt Nov 01 '20 at 13:25