elsamuko
elsamuko

Reputation: 608

How to pass a variable with whitespace to env interpreter

I have this interpreter, which prints the ARGS variable:

#!/bin/bash
echo "[$ARGS]"

I use this interpreter in another script:

#!/usr/bin/env ARGS=first interpreter

Calling the second script, I get

[first]

How do I get

[first second]

?

Upvotes: 3

Views: 435

Answers (1)

mklement0
mklement0

Reputation: 437353

The short of it: don't rely on being able to pass multiple arguments as part of a shebang line, and the one argument you can use must be an unquoted, single word.

For more background information, see the question @tholu has already linked to in a comment (https://stackoverflow.com/a/4304187/45375).

Thus, I suggest you rewrite your other script to use bash as well:

#!/bin/bash

ARGS='first second' /usr/bin/env interpreter "$@"
  • This allows you to use bash's own mechanism for defining environment variables ad-hoc (for the command invoked and its children) by prefixing commands with variable assignments, allowing you to use quoting and even define multiple variables.
  • Whatever command-line arguments were passed in are passed through to interpreter via "$@".

Upvotes: 1

Related Questions