bash edit-and-execute-command echo

Rob*_*ram 6 bash

当我运行以下一行循环时,我得到了预期的输出.

for index in $(seq 1 5) ; do echo "$(date +"%Y%m%d" -d "20160301 $index day")"; done
20160302
20160303
20160304
20160305
20160306
Run Code Online (Sandbox Code Playgroud)

但是当我使用bash的edit-and-execute-command(control- x,control- e)并输入相同的一行循环时,我会得到输出,其中包含意外命令.

for index in $(seq 1 5) ; do echo "$(date +"%Y%m%d" -d "20160301 $index day")"; done
seq 1 5
date +"%Y%m%d" -d "20160301 $index day"
20160302
date +"%Y%m%d" -d "20160301 $index day"
20160303
date +"%Y%m%d" -d "20160301 $index day"
20160304
date +"%Y%m%d" -d "20160301 $index day"
20160305
date +"%Y%m%d" -d "20160301 $index day"
20160306
Run Code Online (Sandbox Code Playgroud)

我有export EDITOR=vim我的.bash_profile.


更新更复杂的例子以回应来自@ l'L'l的评论

我正在使用子shell,因为真正的命令正在做更多的事情.

for index in $(seq 1 10) ; do td=`date +"%Y%m%d" -d "20160301 $index day"`; echo "$td: $(grep "$td" *gprs.csv | wc -l)" ; done
Run Code Online (Sandbox Code Playgroud)

与更简单的示例一样,粘贴到命令行是可以的,但使用edit-and-execute-command会在它们之间产生大量回声.

我已经通过使用脚本来解决这个问题(因为问题变得更加复杂),但是我仍然有兴趣知道是否有一个简单的解决方案(编辑和执行命令似乎没那么有用)如果输出不清楚).

Rus*_*nov 6

Bash在评估临时文件的内容之前打开v标志

/* Turn on the `v' flag while fc_execute_file runs so the commands
   will be echoed as they are read by the parser. */
begin_unwind_frame ("fc builtin");
add_unwind_protect ((Function *)xfree, fn);
add_unwind_protect (unlink, fn);
add_unwind_protect (set_verbose_flag, (char *)NULL);
echo_input_at_read = 1;

retval = fc_execute_file (fn);
run_unwind_frame ("fc builtin");
Run Code Online (Sandbox Code Playgroud)

命令被打印到标准错误描述符。事实证明,我们无法通过标准接口控制它。

但是有可能在 中source的文件中$EDITOR,然后终止编辑器的进程:

#!/bin/bash -
vim "$@"

if [[ "$(basename "$1")" =~ bash-fc-[0-9]+ ]]; then
  # Looks like edit-and-execute (C-x C-e)

  # The user has finished editing the temporary file.
  # It's time to evaluate the contents.
  source "$1"

  # Save current process ID for the subshell below.
  pid=$PID

  # The subshell helps to suppress the `Terminated` message.
  ( kill $pid >/dev/null 2>&1 )
fi
Run Code Online (Sandbox Code Playgroud)

覆盖$EDITOR变量后将不再显示命令:

export EDITOR="/path/to/the-wrapper"
Run Code Online (Sandbox Code Playgroud)