40

You can use \d in the your PS1 confuration to display a long date ie. Tues 18 May, but how can I get it to display it in a format like 18.05.2012 for example?

fergusdawson
  • 1,645
  • 3
  • 17
  • 20

6 Answers6

74

Try including \D{%d.%m.%Y}. You can use any time format supported by strftime(3).

MiniGod
  • 3,683
  • 1
  • 26
  • 27
FatalError
  • 52,695
  • 14
  • 99
  • 116
22

Rather than telling the shell to execute the date command each time, you can use the built-in format:

\D{%F %T}  

to give you date and time in this in format: YYYY-MM-DD hh:mm:ss

Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
user4111240
  • 221
  • 2
  • 2
19
PS1="\$(date +%d.%m.%Y) > "
export PS1
Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
Mithrandir
  • 24,869
  • 6
  • 50
  • 66
19

Use \D{format} where format is a strftime format code. For example:

$ export PS1='\D{%d.%m.%Y}$ '
08.02.2012$
phuclv
  • 37,963
  • 15
  • 156
  • 475
martineg
  • 1,269
  • 13
  • 14
  • Great answer for my problem. However how do you make this permament? – ECII Jun 27 '21 at 07:01
  • Depending on your platform, you could put these kind of customizations in ~/.bash_profile which is read when a new interactive shell is started. – martineg Jun 30 '21 at 13:13
5

you can try this that display time:

$ PS1="\n\t \u@\h:\w# "

08:18:57 user@localhost:/home/user#
Romil Patel
  • 12,879
  • 7
  • 47
  • 76
marwansherin
  • 51
  • 1
  • 1
0

I'm not sure how to apply this to PS1 specifically (maybe someone can edit this question to replace this part with the best working bit from other questions).

BUT you can get short date formatted according to your current locale with:

date +%x
Carolus
  • 477
  • 4
  • 16