I have a text file commands.txt that has over a hundred lines, each of which is a shell command:

echo foo
echo bar

I want to run each line in the file, but I would like to check each line just before running it. Besides, some lines have generic code like

ping <dest_host>

Where I am expected to write an appropriate word for the command. I am trying to find an easy way to run all of these commands in order. Running it like if it were a script is not an option, and copy pasting each line will be a bit tedious.

Any ideas?

  • 2
    Do commands in this file set variables that are used later in the file, and does the file contain multi-line commands, or compound commands and pipelines?
    – Kusalananda
    Commented Nov 30, 2020 at 22:07
  • sounds like a job for the DEBUG trap
    – ilkkachu
    Commented Dec 2, 2020 at 12:11

1 Answer 1


You can have a script named carefully_run.sh with these contents:

while IFS= read -r -u3 cmd
    read -e -i "$cmd" -p "$USER $ " cmd
    eval "$cmd"
done 3<$1

Then you just need to run

carefully_run.sh commands.txt

For each line in commands.txt it will present you a prompt with the contents of the line already written. Then you will have a chance to inspect and modify it however you want, and when you press enter, it will run the command. Then it will present you the next one.


If commands.txt contains a line like


The value of foo will be preserved while running the commands in commands.txt, but it will be lost after the last one. If you need to preserve it, and you happen to be running bash, you can achieve that by running

source carefully_run.sh commands.txt

Instead of

./carefully_run.sh commands.txt
  • To read each line verbatim, you have to while IFS= read -r -u3 cmd. Will xdotool give the user a chance to edit the line first? Maybe this is needed: IFS= read -p "$USER $ " -i "$cmd" -r -e newcmd && eval "$newcmd" Commented Dec 1, 2020 at 3:05
  • 1
    xdotool will just type the command to the console, but it will not "press enter". The user will still be able to edit the line before running
    – Hobber
    Commented Dec 1, 2020 at 10:59
  • OK, so read -i ... -e is certainly an alternative. Commented Dec 1, 2020 at 12:45
  • How could we correctly handle the case where a line contains something like echo "foo\nbar"? One could except to show foo in a line and bar in the next one, but this is not what happens.
    – Hobber
    Commented Dec 1, 2020 at 19:46
  • Also, any ideas on how to implement the same on zsh ?. I don't find any -i and -p equivalents in the man pages
    – Hobber
    Commented Dec 1, 2020 at 20:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .