The commands used to reference entries are described in section 5 ("Referencing Entries in the Document") of the glossaries user manual (texdoc glossaries or CTAN mirror). The commands you are looking for come under the second item "Those that display text in the document without indexing or applying any additional formatting (Section 5.2)."
The commands described in section 5.1 (including \gls) are all robust and far too complicated to expand to simple text (which is what's required when writing to an external file). These include the whatsit created by indexing, formatting commands, hyperlink, and the post-link hook, which can be used to append information or lookahead (e.g. for a following punctuation character).
The commands described in section 5.2, don't have all this extra baggage, but some of them still have non-expandable content. The ones you need are all the non-case-changing \glsentry... commands, such as \glsentrytext.
Example:
\documentclass{article}
\usepackage{glossaries}
\newglossaryentry{sample}{name={sample},
text={sample (next use)},plural={samples (next use)},
first={sample (first use)},firstplural={samples (first use)},
symbol={\S},
description={an example}}
\begin{document}
Robust: \gls{sample}, \gls{sample}, \glsfirst{sample},
\glstext{sample}, \glsname{sample}, \glsdesc{sample},
\glssymbol{sample}. Plurals: \glsfirstplural{sample}, \glsplural{sample}.
\typeout{Robust: \gls{sample}, \gls{sample}, \glsfirst{sample},
\glstext{sample}, \glsname{sample}, \glsdesc{sample},
\glssymbol{sample}. Plurals: \glsfirstplural{sample}, \glsplural{sample}.}
Expandable: \glsentryfirst{sample},
\glsentrytext{sample}, \glsentryname{sample},
\glsentrydesc{sample},
\glsentrysymbol{sample}. Plurals: \glsentryfirstplural{sample},
\glsentryplural{sample}.
Whether or not these cause a problem in expandable contents (such as
writing to a file) depends on whether or not the associated field
has problematic content.
\typeout{Expandable: \glsentryfirst{sample},
\glsentrytext{sample}, \glsentryname{sample},
\glsentrydesc{sample},
\glsentrysymbol{sample}. Plurals: \glsentryfirstplural{sample},
\glsentryplural{sample}.}
You can also save the contents of a field
\glsletentryfield{\foo}{sample}{symbol}
\typeout{Symbol: \expandonce{\foo}}
Or test first if the field is set:
\ifglshasfield{symbol}{sample}
{Symbol: \glscurrentfieldvalue.
\typeout{Symbol: \expandonce\glscurrentfieldvalue}}
{No symbol.}
\end{document}
This produces the following in the transcript:
Robust: \gls {sample}, \gls {sample}, \glsfirst {sample}, \glstext {sample}, \glsname {sample}, \glsdesc {sample}, \glssymbol {sample}. Plurals: \glsfirstplural {sample}, \glsplural {sample}.
Expandable: sample (first use), sample (next use), sample, an example, \S . Plurals: samples (first use), samples (next use).
Symbol: \S
Symbol: \S
Be careful of the last example in the above MWE. If the write is delayed the definition of \glscurrentfieldvalue may have changed by the time the write is actually performed. The conditional \ifglshasfield is described in section 5.12 ("Conditionals").
texdoc glossaries-code) – David Carlisle Feb 20 '22 at 12:25\glscommand. – zetyty Feb 24 '22 at 20:21