When you type write(X).
at the interactive prompt, and nothing else, X is not bound to anything in particular. If you want to read X from the user and then write it, try typing read(X), write(X).
at the prompt.
?- read(X), write(X).
|: 28.
28
X = 28.
SWI Prolog does keep a history of top-level bindings; type help.
to go into the manual, then search for bindings
or just navigate to section 2.8 of the manual, 'Reuse of top-level bindings'. There, you can learn that the most recent value of any variable bound in a successful top-level goal is retained, and can be referred to using the name of the variable, prefixed with a dollar sign. So interactions like the following are possible:
?- read(X).
|: 42.
X = 42.
?- write($X).
42
true.
But a top-level goal that just happens to use the variable name X will be interpreted as using a fresh variable; to do otherwise would violate the normal semantics of Prolog.