Bash terminal [\t] (time) is not same timezone as ‘date’

bashtimetimezone

In my .bashrc file I configured how I would like my prompt to be displayed. My .bashrc is sourced in .bash_profile. The command in the .bashrc file is:

PS1='[\t] \u@\h is $PWD :: '

I switched the user (for me) timezone to GMT+0 using tzselect. The problem I am having is that using the date command I am getting the GMT time (which I am pleased with) but the terminal prompt is still on the system time (even after logging in and logging out), which I am not pleased with.

How can I get the terminal to display the time in the time format that I, the user, am specifying?

Best Answer

You don't say how you set the timezone, but since you set it for your own user, that has to be by setting the TZ variable. Bash (at least version 4.1.5) has some quirks when it comes to taking a TZ change into account: the change is only reflected after the shell has started an external command (it has to be an external command, forking a subshell isn't enough, nor is executing built-ins).

bash-4.1$ PS1='\t '
12:31:05 export TZ=Asia/Tokyo
12:31:12
12:31:13 echo foo
foo
12:31:14 /bin/echo foo
foo
19:31:17

So if you set TZ in your .bash_profile and you don't happen to start an external command, you will still see the time in the old timezone at the first prompt.

export TZ=GMT
# Refresh bash's idea of $TZ for prompt display purposes.
# As of bash 4.1, this requires running an external command!
if [[ $- = *i* ]]; then command true; fi
Related Question