4

Okay, so I'm stuck working with a shell script that needs to use eval (I know, I know, ultimate evil etc…) however I'm having difficulty debugging some of the line numbers being thrown out by errors.

For example:

script.sh: eval: line 315: syntax error near unexpected token `('

The line definitely doesn't correspond to line 315 in script.sh as that's entirely unrelated to the error. So I threw in random new-lines to figure out which line is originating the error, and as you'd expect it's a line with an eval statement.

However, when I output the code being passed into the eval, it doesn't even contain 200 lines, let alone 315+! So what's going on here? Is there a way to usefully use the line numbers being reported, are they somehow based on the position at which the eval statement is run or something? I don't seem to be able to see a pattern thus far.

If I could I'd replace all the eval statements, but unfortunately the code being passed is dynamically generated (not from user-input, don't worry!), and needs to be able to manipulate variables in the same scope as the rest of the script, which seems to preclude using echo "$commands" | sh or similar alternatives. I also have the issue of compatibility, and unfortunately as evil as it is eval is widely available.

  • 1
    Could you post a minimal working example that reproduces the issue? – terdon Dec 10 '13 at 16:02
  • 1
    yes, line numbers when using eval are random with bash, try to interpret the script with a compatible shell, zsh in bash emulation mode and ksh93 come to mind. – Stéphane Chazelas Dec 10 '13 at 16:06
  • What if you execute your script with printing line numbers for all executed lines instead of just getting the line number where the error happened. Like here: stackoverflow.com/a/17805088/184968. It might give you an idea what is wrong with the script. So you run your script in this way: PS4='$0.${LINENO}: ' bash -x script.sh – Sergei Kurenkov Dec 10 '13 at 18:43
  • Please show the lines of code that have eval. – agc Oct 14 '16 at 2:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.