Bash 将多个命令组合成一行

Jus*_*tin -2 linux unix bash

有没有办法将以下内容组合成一个漂亮的“单衬”:

echo "free -m" > /tmp/dC4v2cK
bash /tmp/dC4v2cK
Run Code Online (Sandbox Code Playgroud)

Zor*_*che 5

我认为您正在寻找eval(如foo='free -m'; eval $foo)。请记住,如果$foo来自不受信任的来源,那么做这样的事情会带来安全隐患,那么您可能会无意中做坏事。例如,如果有人设法让这条线发生怎么办。 foo='rm -rf /'

$ help eval
eval: eval [arg ...]
    Execute arguments as a shell command.

    Combine ARGs into a single string, use the result as input to the shell,
    and execute the resulting commands.

    Exit Status:
    Returns exit status of command or success if command is null.
Run Code Online (Sandbox Code Playgroud)


wom*_*ble 5

或者如果你真的想要一个子shell:

/bin/bash -c "free -m"
Run Code Online (Sandbox Code Playgroud)