echo命令,然后运行它? (喜欢化妆) [英] Echo command, and then run it? (Like make)

查看:165
本文介绍了echo命令,然后运行它? (喜欢化妆)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让庆典成了某种详细模式在哪里,这样,当它运行一个shell脚本,它呼应了它会运行之前运行的命令?也就是说,这样才可能看到已运行(以及它们的输出)的命令,类似于的输出使

也就是说,如果运行像

的shell脚本

 回声你好,世界

我想下面的输出

 回声你好,世界
你好,世界

此外,是有可能写出一个叫bash函数 echo_and_run 将输出一个命令,然后运行它?

  $ echo_and_run回声你好,世界
回声你好,世界
你好,世界


解决方案

这是可能的使用bash的的printf 会同在%Q 格式说明逃跑的参数,以便空间是preserved:

 功能echo_and_run {
  回声$$ @
  的eval $(printf的'%Q'$ @)<为/ dev / tty的
}

Is there some way to get bash into a sort of verbose mode where, such that, when it's running a shell script, it echoes out the command it's going to run before running it? That is, so that it's possible to see the commands that were run (as well as their output), similar to the output of make?

That is, if running a shell script like

echo "Hello, World"

I would like the following output

echo "Hello, World"
Hello, World

Alternatively, is it possible to write a bash function called echo_and_run that will output a command and then run it?

$ echo_and_run echo "Hello, World"
echo "Hello, World"
Hello, World

解决方案

It's possible to use bash's printf in conjunction with the %q format specifier to escape the arguments so that spaces are preserved:

function echo_and_run {
  echo "$" "$@"
  eval $(printf '%q ' "$@") < /dev/tty
}

这篇关于echo命令,然后运行它? (喜欢化妆)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆