How to run script commands from variables?

后端 未结 3 1185
感情败类
感情败类 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:24

    You need a heredoc to do this correctly. In answer to POSIX compliant way to see if a function is defined in an sh script, I detailed how to read a script into a variable, programmatically parse it for information and/or modify it as necessary, then execute it from another script or shell function. That's basically what you're trying to do, and the heredoc makes it possible because it provides a file descriptor:

    % multi='ls -l | grep e'
    % sh <<_EOF_
    > ${multi}
    > _EOF_
    < desired output >
    

    That would solve your simple example case. See my other answer for more.

    -Mike

提交回复
热议问题