1

I'm trying to run a tcpdump command in a bash script, but I keep getting this error : tcpdump: illegal token: "

My script can be summarized by :

a='sudo tcpdump "icmp"' echo $a $a

When I run it the echo $a print sudo tcpdump "icmp", but when $a is executed, I get the error. When I copy paste the printed message, the command just run normally.

I also tried to put the command in an array and execute it by calling : ${array[@]}, but it didn't solve the problem.

Does someone have a solution?

  • 4
    Use a='sudo tcpdump icmp'. The shell doesn't re-parse what it has already parsed; after expanding $a it does not go again over the expanded text and re-parse it; as a result, the shell does not strip away the " characters; tcpdump sees the " character and complains. – AlexP Nov 15 '17 at 14:40
0

try

eval "$a"

according to man bash

   eval [arg ...]
          The args are read and concatenated together into a single command.  This command is then read and executed by the shell, and its exit status is returned  as  the  value  of
          eval.  If there are no args, or only null arguments, eval returns 0.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.