fergusdawson
fergusdawson

Reputation: 1765

Short date in bash PS1 prompt

You can use \d in the your PS1 configuration 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?

Upvotes: 48

Views: 27232

Answers (6)

Mithrandir
Mithrandir

Reputation: 25397

PS1="\$(date +%d.%m.%Y) > "
export PS1

Upvotes: 20

user4111240
user4111240

Reputation: 261

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

Upvotes: 26

martineg
martineg

Reputation: 1359

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

$ export PS1='\D{%d.%m.%Y}$ '
08.02.2012$

Upvotes: 21

FatalError
FatalError

Reputation: 54621

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

Upvotes: 83

Carolus
Carolus

Reputation: 570

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

Upvotes: 0

marwansherin
marwansherin

Reputation: 51

you can try this that display time:

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

08:18:57 user@localhost:/home/user#

Upvotes: 5

Related Questions