Bash echo, echo and end and end

When I run the next loop, I get the expected result.

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

But when I use bash edit-and-execute-command ( control- x, control- e) and enter the same loop, I get output with unexpected commands that echo repeated.

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

I have export EDITOR=vimin mine .bash_profile.


Update with a more complex example in response to a comment from @ l'L'l

I use sub-shells because a real team makes a fair bit bigger.

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

As with the simpler example, pasting into the command line is OK, but using the edit-and-execute-command gives a lot of echoes between them.

script ( , , ), , ( edit-and-execute -nnn 't , ).

+4
1

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");

. , .

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 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

$EDITOR:

export EDITOR="/path/to/the-wrapper"
+2
source

Source: https://habr.com/ru/post/1650867/


All Articles