在bash中的每个命令后执行命令

时间:2022-04-27 04:45:41

I want to print the date after every bash command I run.

我想在运行每个bash命令后打印日期。

This could help me understand how much a command took to execute when I am away from keyboard.

这可以帮助我理解当我离开键盘时命令执行了多少。

I know I could do

我知道我能做到

`DATE=`date +%d/%m/%Y\ %H:%M:%S` && echo $DATE`

to get the date but I don't know how or even if it could be possible to run this command after every command I execute on bash.

获取日期,但我不知道在bash上执行的每个命令之后是否可以运行此命令。

I would also be interested in running the same command before every command so I could know how long a command took.

我也有兴趣在每个命令之前运行相同的命令,这样我就可以知道命令花了多长时间。

Is it possible?

可能吗?

What file should I edit?

我应该编辑什么文件?

For example:

$ wget google.com
15/07/2017 23:40:05

I would be happy, if I could also introduce this following feauture:

我很高兴,如果我也可以介绍以下特色:

$ wget google.com
15/07/2017 23:40:05 15/07/2017 23:40:11 
Program run for 00:00:06

where the first date is when I ran the program, second is when program terminated the third is self-explanatonary.

第一个日期是我运行程序的地方,第二个是程序终止时第三个是自我解释的。

As you understood, I don't want to type every time

如你所知,我不想每次都打字

$ wget google.com && `DATE=`date +%d/%m/%Y\ %H:%M:%S` && echo $DATE`

4 个解决方案

#1


6  

To execute a cmd before every command entered, set a trap on DEBUG. Eg.

要在输入每个命令之前执行cmd,请在DEBUG上设置陷阱。例如。

trap date DEBUG

To execute that command before emitting a prompt, set PROMPT_COMMAND:

要在发出提示之前执行该命令,请设置PROMPT_COMMAND:

PROMPT_COMMAND=date

#2


4  

This does exactly that:

这恰恰是这样的:

PROMPT_COMMAND+=$'\n'"date +%d/%m/%Y\ %H:%M:%S"

The string in PROMPT_COMMAND gets evaluated after every command. You just need to add the date command to whatever you already had in it. ($'\n' (newline) is a somewhat more robust joiner than ; as two consecutive ; would give you a syntax error)

PROMPT_COMMAND中的字符串在每个命令后得到评估。您只需要将date命令添加到其中已有的任何内容中。 ($'\ n'(换行符)是一个比一个更强大的连接器;因为连续两个;会给你一个语法错误)

#3


2  

You can add date/time to your prompt, via PS1 variable. You could use date command, but it's more efficient to use the supported special characters, like \d for date, or \D{strftime-fmt}.

您可以通过PS1变量为提示添加日期/时间。您可以使用日期命令,但使用支持的特殊字符更有效,例如\ d表示日期,或\ D {strftime-fmt}。

For example:

PS1='\u@\h[\D{%F} \D{%T}]\w\$ '

or, with color:

或者,有颜色:

PS1='\[\033[01;32m\]\u@\h\[\033[00m\][\[\033[02;33m\]\D{%F}\[\033[08m\]T\[\033[00m\]\[\033[02;33m\]\D{%T}\[\033[00m\]]\[\033[01;34m\]\w\[\033[00m\]\$ '

will show:

user@host[2017-07-16 00:01:17]~/somedir$

Note that in the second case (with color) we have a valid ISO8601 timestamp, with a "hidden" date/time separator T in the middle. If you select it with a mouse, T is visible and can be copied. (Also double-click will select the complete timestamp, not only date or time.)

请注意,在第二种情况下(带颜色),我们有一个有效的ISO8601时间戳,中间有一个“隐藏”日期/时间分隔符T.如果使用鼠标选择它,则可以看到T并可以复制。 (同时双击将选择完整的时间戳,而不仅仅是日期或时间。)

#4


0  

To print timestamp after every command just modify your PS1 prompt and add date to it. The only catch here is that it will tell you time when command ended and new prompt showed. So in case you have your prompt open for long time just hit enter to capture start time before running your command.

要在每个命令后打印时间戳,只需修改PS1提示并向其添加日期即可。这里唯一的问题是它会告诉你命令何时结束并显示新提示的时间。因此,如果您的提示打开很长时间,只需按Enter键以捕获开始时间,然后再运行命令。

PS1="\D{%F %T} \$ "

See this arch wiki page or just google bash prompt customization.

查看此arch wiki页面或只是谷歌bash提示自定义。

To add time spent executing program just add time before the command

要增加执行程序所花费的时间,只需在命令之前添加时间

$ time wget google.com

It will give you output like this

它会给你这样的输出

real 0m0.177s
user 0m0.156s
sys 0m0.020s

And you can get even more lazy and for commands that you dont't feel like typing time every time you run it, just create alias.

而且你可以变得更加懒惰,并且对于每次运行时都不想输入时间的命令,只需创建别名即可。

alias wget="time wget"

Because in bash aliases are run before other commands you can do it this way even if it looks like recursion. Then you will call it as you are used to.
And of course, aliases and prompt settings can be put in your .bashrc file, so you don't have to type them every time you open terminal.

因为在bash别名是在其他命令之前运行的,你可以这样做,即使它看起来像递归。然后你将按照你习惯的方式调用它。当然,别名和提示设置可以放在.bashrc文件中,因此每次打开终端时都不必输入它们。

#1


6  

To execute a cmd before every command entered, set a trap on DEBUG. Eg.

要在输入每个命令之前执行cmd,请在DEBUG上设置陷阱。例如。

trap date DEBUG

To execute that command before emitting a prompt, set PROMPT_COMMAND:

要在发出提示之前执行该命令,请设置PROMPT_COMMAND:

PROMPT_COMMAND=date

#2


4  

This does exactly that:

这恰恰是这样的:

PROMPT_COMMAND+=$'\n'"date +%d/%m/%Y\ %H:%M:%S"

The string in PROMPT_COMMAND gets evaluated after every command. You just need to add the date command to whatever you already had in it. ($'\n' (newline) is a somewhat more robust joiner than ; as two consecutive ; would give you a syntax error)

PROMPT_COMMAND中的字符串在每个命令后得到评估。您只需要将date命令添加到其中已有的任何内容中。 ($'\ n'(换行符)是一个比一个更强大的连接器;因为连续两个;会给你一个语法错误)

#3


2  

You can add date/time to your prompt, via PS1 variable. You could use date command, but it's more efficient to use the supported special characters, like \d for date, or \D{strftime-fmt}.

您可以通过PS1变量为提示添加日期/时间。您可以使用日期命令,但使用支持的特殊字符更有效,例如\ d表示日期,或\ D {strftime-fmt}。

For example:

PS1='\u@\h[\D{%F} \D{%T}]\w\$ '

or, with color:

或者,有颜色:

PS1='\[\033[01;32m\]\u@\h\[\033[00m\][\[\033[02;33m\]\D{%F}\[\033[08m\]T\[\033[00m\]\[\033[02;33m\]\D{%T}\[\033[00m\]]\[\033[01;34m\]\w\[\033[00m\]\$ '

will show:

user@host[2017-07-16 00:01:17]~/somedir$

Note that in the second case (with color) we have a valid ISO8601 timestamp, with a "hidden" date/time separator T in the middle. If you select it with a mouse, T is visible and can be copied. (Also double-click will select the complete timestamp, not only date or time.)

请注意,在第二种情况下(带颜色),我们有一个有效的ISO8601时间戳,中间有一个“隐藏”日期/时间分隔符T.如果使用鼠标选择它,则可以看到T并可以复制。 (同时双击将选择完整的时间戳,而不仅仅是日期或时间。)

#4


0  

To print timestamp after every command just modify your PS1 prompt and add date to it. The only catch here is that it will tell you time when command ended and new prompt showed. So in case you have your prompt open for long time just hit enter to capture start time before running your command.

要在每个命令后打印时间戳,只需修改PS1提示并向其添加日期即可。这里唯一的问题是它会告诉你命令何时结束并显示新提示的时间。因此,如果您的提示打开很长时间,只需按Enter键以捕获开始时间,然后再运行命令。

PS1="\D{%F %T} \$ "

See this arch wiki page or just google bash prompt customization.

查看此arch wiki页面或只是谷歌bash提示自定义。

To add time spent executing program just add time before the command

要增加执行程序所花费的时间,只需在命令之前添加时间

$ time wget google.com

It will give you output like this

它会给你这样的输出

real 0m0.177s
user 0m0.156s
sys 0m0.020s

And you can get even more lazy and for commands that you dont't feel like typing time every time you run it, just create alias.

而且你可以变得更加懒惰,并且对于每次运行时都不想输入时间的命令,只需创建别名即可。

alias wget="time wget"

Because in bash aliases are run before other commands you can do it this way even if it looks like recursion. Then you will call it as you are used to.
And of course, aliases and prompt settings can be put in your .bashrc file, so you don't have to type them every time you open terminal.

因为在bash别名是在其他命令之前运行的,你可以这样做,即使它看起来像递归。然后你将按照你习惯的方式调用它。当然,别名和提示设置可以放在.bashrc文件中,因此每次打开终端时都不必输入它们。