Bash - how to find the current shell command

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

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

I tried using echo !! inside .bashrc but that just gives me !! like a headline. Any ideas?

+4
source share
3 answers

If you are trying to update the xterm header, you can use the DEBUG trap:

 trap 'echo "$BASH_COMMAND"' DEBUG 

See this post.

+6
source

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

 #!/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##*/} " 
+1
source

okay - now that you have clarified your question, I offer a different answer.

In fact, the value you want is not available as an environment variable, but what about this:

 tail -n 1 $HOME/.bash_history 

Am I getting warmer? :)

edit:

Note that if you want to use this in your PROMPT_COMMAND , you need to do this:

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

hope this helps :)

+1
source

Source: https://habr.com/ru/post/1340781/


All Articles