6

When I run a command, I need to set some shell environment variable that holds the current command from inside ".bashrc". Actually I need to update PROMPT_COMMAND whenever a command is run, and I need the whole command line, from where I will pick relevant value.

PROMPT_COMMAND='TITLE=`echo !!`; echo $TITLE;'

I tried using echo !! inside .bashrc but this simply gives me !! as title. Any ideas?

Neo
  • 13,179
  • 18
  • 55
  • 80

3 Answers3

10

If you're trying to update the title of the xterm, you can use a DEBUG trap:

trap 'echo "$BASH_COMMAND"' DEBUG

See this blog post.

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • ha! - just added an answer based on this to @Neo's main question, [here](http://stackoverflow.com/questions/5076127/bash-update-terminal-title-by-running-a-second-command/5080670#5080670) ! -- edit: the link you post has an improvement on my solution, so thanks! +1 – simon Feb 22 '11 at 16:12
  • Thanks a lot. In the end it was so simple. I was almost going to recompile my shell, because apparently COMP_LINE variable is available only for shell functions. – Neo Feb 22 '11 at 18:59
5

not sure exactly what you need, but it should be in here -- try it :)

#!/bin/bash

echo "# arguments called with ---->  ${@}     "
echo "# \$1 ----------------------->  $1       "
echo "# \$2 ----------------------->  $2       "
echo "# path to me --------------->  ${0}     "
echo "# parent path -------------->  ${0%/*}  "
echo "# my name ------------------>  ${0##*/} "
simon
  • 15,344
  • 5
  • 45
  • 67
2

okay - now that you've clarified your question, I'll offer a different answer.

Actually, the value you want isn't available as an environment variable, but how about this:

tail -n 1 $HOME/.bash_history

am I getting warmer? :)

edit:

note, if you want to use this in your PROMPT_COMMAND, what you'll need to do it this:

export PROMPT_COMMAND='history -a; tail -n 1 $HOME/.bash_history'

hope this helps :)

simon
  • 15,344
  • 5
  • 45
  • 67
  • 2
    I tried this already :). The problem is, `history` gets updates once a command gets executed and finished. But in case you have a long running process, `history` will not get updated with current command. – Neo Feb 22 '11 at 12:34
  • if you have a long-running process, PROMPT_COMMAND won't be executed until it's finished... am I to understand that you want some kind of indication of **background** processes, or something like that? sorry -- still not too clear! – simon Feb 22 '11 at 12:38
  • you are right, PROMPT_COMMAND will executed after the completion. Well, actually I needed to update the title of my shell with name of the current command. I posted the original problem here http://stackoverflow.com/questions/5076127/bash-update-terminal-title-by-running-a-second-command/5076176#5076176 . So I guess I need another way out. – Neo Feb 22 '11 at 13:00
  • ahh - now that I see what you really want, I've posted a much fuller answer on your other question :) – simon Feb 22 '11 at 16:10