bash - 如何找到当前的 shell 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5077836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:30:19  来源:igfitidea点击:

bash - how to find current shell command

bashenvironment-variableshistory

提问by Neo

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.

当我运行一个命令时,我需要设置一些 shell 环境变量来保存“.bashrc”中的当前命令。实际上,无论何时运行命令,我都需要更新 PROMPT_COMMAND,并且我需要整个命令行,从中我将选择相关值。

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

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

我尝试echo !!在 .bashrc 中使用,但这只是给了我!!标题。有任何想法吗?

回答by Paused until further notice.

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

如果您尝试更新 xterm 的标题,则可以使用 DEBUG 陷阱:

trap 'echo "$BASH_COMMAND"' DEBUG

See this blog post.

请参阅此博客文章

回答by simon

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:

请注意,如果你想在你的 中使用它,你PROMPT_COMMAND需要这样做:

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

hope this helps :)

希望这可以帮助 :)

回答by simon

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

不确定你到底需要什么,但它应该在这里——试试吧:)

#!/bin/bash

echo "# arguments called with ---->  ${@}     "
echo "# $1 ----------------------->         "
echo "# $2 ----------------------->         "
echo "# path to me --------------->  ##代码##     "
echo "# parent path -------------->  ${0%/*}  "
echo "# my name ------------------>  ${0##*/} "