0

Suppose I have this in script.sh:

env -i SOMEVAR=SOMEVALUE eval -- "$@"

I run it with:

./script.sh echo "\$SOMEVAR"

Now it shows:

env: ‘eval’: No such file or directory

I suppose it doesn't work because eval is a Bash builtin.

Any way to make env work with eval?

3
  • env expect a command (so on PATH), but you provide with a keyword. Just replace it with sh eval. Commented Sep 12, 2022 at 13:48
  • 2
    @GiacomoCatenazzi If using sh, why bother with eval at all?
    – Kusalananda
    Commented Sep 12, 2022 at 13:49
  • @Kusalananda: you are totally correct. I was just giving the generic answers, and I miss that to add that this is special. Commented Sep 12, 2022 at 13:52

1 Answer 1

1

Why do you even want eval? Just use the shell:

$ cat foo.sh
#!/bin/sh
env -i SOMEVAR=SOMEVALUE  sh -c "$@"

Then you run it like this:

$ foo.sh 'echo $SOMEVAR'
SOMEVALUE
5
  • Can I call the script without the quotes? (i.e. foo.sh echo '$SOMEVAR')
    – sudoer
    Commented Sep 12, 2022 at 14:19
  • @sudoer if you change sh -c "$@" to sh -c "$*", yes. But it is better to just make sure you quote things.
    – terdon
    Commented Sep 12, 2022 at 14:23
  • Another problem: The arguments supplied to the script must not have spaces. Fix?
    – sudoer
    Commented Sep 12, 2022 at 14:26
  • @sudoer yes, quote your input. Or ask a new question and show the actual command you want to run and explain why you are using such a complicated approach. I suspect this is an XY problem and if you tell us why you want this we can give you a better solution.
    – terdon
    Commented Sep 12, 2022 at 14:28
  • I think i found a solution. But I am unavailable to try it out now. I'll test it tmr :)
    – sudoer
    Commented Sep 12, 2022 at 14:41

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.