How to run script commands from variables?

后端 未结 3 1128
感情败类
感情败类 2021-01-04 20:54

I tried to run commands using pipes.

Basic:

single=\"ls -l\"
$single

which works as expected

Pipes

3条回答
  •  迷失自我
    2021-01-04 21:33

    You're demonstrating the difference between the shell and the kernel.

    "ls -l" is executable by the system execve() call. You can man execve for details, but that's probably too much detail for you.

    "ls -l | grep e" needs shell interpretation to set up the pipe. Without using a shell, the '|' character is just passed into execve() as an argument to ls. This is why you see the "No such file or directory" errors.

    Solution:

    cmd="ls -l | grep e"
    bash -c "$cmd"
    

提交回复
热议问题