bashshelloutputprompt

Execute a shell script as it would be an interactive session


For documentation purposes I am trying to execute a shell script in a way that it looks as you typed it by hand in an interactive shell.

Script:

x=123
echo $x

Then execute:

PS4="$PS1"
set -x -v
. ./demo

Output:

. ./demo
user@host:~/tmp$ . ./demo
x=123
user@host:~/tmp$ x=123
echo $x
user@host:~/tmp$ echo 123
123

Desired output:

user@host:~/tmp$ x=123
user@host:~/tmp$ echo $x
123

It does not have to be bash. Any solution that simulates an interactive session is welcome.

How can I achieve the desired result?


Solution

  • A pretty simple solution is to start an interactive shell and redirect your script file to the input stream:

    bash -i </path/to/script-file