Is there a way to intercept every command specified in bash ? I can intercept a specific command, for example, cd , by defining a cd() function, and I can do this for one command in time for other commands. But can I write a function that is called before each command? I want to execute some commands and then execute the command.
Michal Šrajer idea PS4='$(echo $(date) $(history 1) >> /tmp/trace.txt) TRACE: ' looks very promising, but here is the result that I get:
$ ping www.google.com TRACE: ping www.google.com PING www.l.google.com (74.125.224.52) 56(84) bytes of data. 64 bytes from 74.125.224.52: icmp_seq=1 ttl=56 time=3.77 ms 64 bytes from 74.125.224.52: icmp_seq=2 ttl=56 time=2.33 ms ^C --- www.l.google.com ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1001ms rtt min/avg/max/mdev = 2.334/3.054/3.774/0.720 ms TRACE: echo -ne '\033]0;myhost.com /home/yogeshwer/github/myproject\007' TRACE: grep -e '\* ' TRACE: git branch TRACE: sed 's/^..\(.*\)/ {\1}/'
And Wed Aug 3 12:47:27 PDT 2011 6672 ping www.google.com written to /tmp/trace.txt exactly 5 times. Four others from my definition of PS1 , where I run the following command: $(git branch 2> /dev/null | grep -e "\* " | sed "s/^..\(.*\)/ {\1}/") . Two questions:
- Is it possible to write the
/tmp/trace.txt command /tmp/trace.txt ? - And more importantly, is it possible not to clutter up the output of the command, but just write the command
/tmp/trace.txt ?
I am very happy to be able to record commands from all my bash sessions in one place!
Yogeshwer sharma
source share