I am looking for a graceful way to have bash write both the command I executed, followed by the output of that command to a file from an interactive prompt. Such that running a command like this:
$ls -alh > list_dir
would write something resembling this to list_dir:
$ls -alh > list_dir total 12K drwxr-xr-x 2 root root 4.0K Dec 21 13:30 . drwx------ 5 root root 4.0K Dec 21 13:30 .. -rw-r--r-- 1 root root 842 Dec 21 13:09 file -rw-r--r-- 1 root root 0 Dec 21 13:29 file1 -rw-r--r-- 1 root root 0 Dec 21 13:29 file2
I have found many articles with solutions that claim to work within a script, but nothing that has worked from an interactive shell. For example, many people suggest adding this toward the top of a script:
If I enter this in my shell, the commands are printed (although not nicely) to standard output, but I have not managed to figure out how to include them in output redirection.
I also saw many people suggest using script to do this. This is the closest I've come to finding what I'm looking for, but I don't always want EVERYTHING to be recorded. I want to be in control of which commands are redirected to a file.
Does anyone know of a good solution to this? I'm open to more out-of-the-box ideas so long as they're not too clunky for regular use.