How to add the output of a command to the Bash prompt?

Project:Linux software
Component:Documentation
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active
Related pages:#5655: French Republican Calendar :-:-: #5663: bash prompt
Tags:bash prompt
Description

How to get the output of a command within the bash prompt?

I tried something simply like the following, but the time is not being updated. The times is obviously only calculated once, when PS1 is being evaluated:

$ PS1="$(date +%H:%M:%S) $ "
18:42:27 $
18:42:27 $
18:42:27 $
18:42:27 $
18:42:27 $
18:42:27 $
18:42:27 $ PS1="$(date +%H:%M:%S) $ "
18:42:45 $

How to make it so that the command is being evaluated each time?

I now I can use \t for the current time, but above is only an example. I actually want something like the following output:

$  PS1="$(t=`date +%s` ; let t=t+8*3600;  perl -MDateTime::Calendar::FrenchRevolutionary -le "print DateTime::Calendar::FrenchRevolutionary->from_epoch(epoch => $t)->strftime('%c')") >  "
Tri 23 Plu 0224 7:84:62 > 

Tri 23 Plu 0224 7:84:62 > 
Tri 23 Plu 0224 7:84:62 > 
Tri 23 Plu 0224 7:84:62 > 
Tri 23 Plu 0224 7:84:62 > 
Tri 23 Plu 0224 7:84:62 > 
Tri 23 Plu 0224 7:84:62 > 
Tri 23 Plu 0224 7:84:62 > 
Tri 23 Plu 0224 7:84:62 > 
Tri 23 Plu 0224 7:84:62 > 
Tri 23 Plu 0224 7:84:62 >   PS1="$(t=`date +%s` ; let t=t+8*3600;  perl -MDateTime::Calendar::FrenchRevolutionary -le "print DateTime::Calendar::FrenchRevolutionary->from_epoch(epoch => $t)->strftime('%c')") >  "
Tri 23 Plu 0224 7:85:17 > 
Tri 23 Plu 0224 7:85:17 > 
Tri 23 Plu 0224 7:85:17 > 
Tri 23 Plu 0224 7:85:17 > 
Tri 23 Plu 0224 7:85:17 > 

Comments

#1

Oh! The answer is easy. Simply escape the $ thus: \$

$ PS1="\$(date +%H:%M:%S) $ "
18:52:32 $
18:52:33 $
18:52:34 $
18:52:34 $
18:52:36 $

#2

There was some escaping problem with the long command, so the easiest is to create a function:

$ function french_republican_time { t=`date +%s` ;  let t=t+8*3600;  perl -MDateTime::Calendar::FrenchRevolutionary -le "print DateTime::Calendar::FrenchRevolutionary->from_epoch(epoch => $t)->strftime('%c')"; }
$ PS1="\$(french_republican_time) $ "
Tri 23 Plu 0224 7:89:90 $
Tri 23 Plu 0224 7:89:90 $
Tri 23 Plu 0224 7:89:91 $
Tri 23 Plu 0224 7:89:92 $