Sorry for the confusing title!
Suppose I run
apt-cache depends kde-window-manager > ~/Desktop/kwin-depends
I'll get a file named "kwin-depends" in my Desktop folder.
Is there some trick to include the command I issued as part of the file, preferably at the start of the file?
So, at least in 14.04 LTS, the first few lines would look like this:
apt-cache depends kde-window-manager > ~/Desktop/kwin-depends
kde-window-manager
Depends: kde-runtime
Depends: libc6
|Depends: libegl1-mesa
Depends: <libegl1-x11>
Instead of just like this:
kde-window-manager
Depends: kde-runtime
Depends: libc6
|Depends: libegl1-mesa
Depends: <libegl1-x11>
I would just use a simple function. Add this to your
~/.bashrc
file:Now, whenever you want to run a command and print it, you can do:
The above produces this file:
Most minimalistic - approach #4 and #3, both could be converted into function; #2 my favorite -
awk
. #1 usesscript
command - very versatile tool, useful for recording command line in general; applicable anywhere, for whatever you want to record.Approach #1: There is a
/usr/bin/script
command ( which comes with ubuntu by default ) for recording command-line output, which captures everything , along with the prompt and command. To just save one command and its output to specific file, use-c
flag and specify output file. ExampleApproach #2: awk hackery
Awk has
system()
function which allows you running shell commands fromawk
script or command. The output will show up on the screen , command first, output next. To redirect what you see to a file use>
operator.That can be done in two ways - ask user to input stuff from stdin or as command line argument. First one is easier to achieve, hence posting that.
(1)
awk 'BEGIN{ print "Enter command to run: "; getline com < "/dev/stdin"; system(com) }'
(2) Command line args version; not including output to avoid making answer too long. Again, append
>
to redirect to fileApproach #3: ask bash to do the job for you
Redirect to file with
>
operator:bash -c ' MYCOMMAND="apt-cache depends gnome-terminal"; echo $MYCOMMAND ; $MYCOMMAND ' > output.txt
Approach #4:(my second favorite)
Inspired by ByteCommander's post; we can use
read
and then run necessary commands in subshellSample run:
Approach #5:
Use
echo
orhere string
(aka<<< "string"
) to provide arguments tosh -c
throughxargs
And if you want , you can use this same trick with an alias:
You can do:
Same thing using
echo
instead of Here strings (<<<
):tee
will write to STDOUT and also to the filefile.txt
The STDOUT of
tee
i.e.apt-cache depends kde-window-manager
will be fed tobash
to run the command and append the STDOUT tofile.txt
.Example:
script -q outputfile
outputfile
Example
Start
script
Start your command
Press Ctrl-D
Show your command and output
and you will see something like this
If you want alias expansion (bash only) you can do it this way :
You can now run
There may be an easier way, but you can do it by a script:
Create a file
script
with this content in your Home folder and give execution permissionRun it this way:
Extremely simple solution using a one-line Bash function
Preparation:
This approach uses a custom bash function to provide the desired functionality. You define it by executing the following line in your terminal session. note that you may chose any valid bash variable name instead of
runandlog
:This however only persists for the current Bash session, that means after closing the terminal window, the function will be gone.
If you tried and liked it though, you can make it always available by editing your
~/.bashrc
file and appending this line to the end of it.How to use:
After having defined the function, you may use it to run commands while logging both the command itself and its output to a file. You could even add more information like the user who executed it, which I already included into the function, or exact time it was ran. Feature-requests in comments are welcome! :)
The syntax is simple:
Example:
An example session as user
bytecommander
, operating from the home directory could look like this:Which will result in a new file
mylogfile
(if it already exists, the function will append the output to it!) in the current directory with the content:A fairly unclever but functional trick would be:
Ugly, but it works!
You could simply pass the command to a function which will print the command first and the command's output afterwards (the redirections are kept out of the printed command intentionally, you may easily change this by removing the quotes from the command and by printing and running
$@
instead of$1
in the function):To add the command afterwards you could run this command, which will insert the last command run at the top of a file:
<<<"[...]"
: here string;[...]
is redirected tocat
'sstdin
;$(<foo)
: command substitution; it's replaced with "foo"'s content;cat [...] - >foo
: concatenatesstdin
to[...]
and outputs to "foo";<([...])
: process substitution: it's replaced with a file descriptor containing the output of[...]
;history 2 | sed -n '1s/ [0-9][0-9]* \(.*\)/\1\n/p'
: outputs the last two commands, removes two spaces followed by one or more digits followed by two spaces from the first line and prints it;