os.Getenv
Returns the value of an environment variable, or an empty string if the environment variable is not set.
- Syntax
- os.Getenv VARIABLE
- Returns
- string
- Alias
- getenv
Security
By default, when using the os.Getenv function Hugo allows access to:
- The
CIenvironment variable - Any environment variable beginning with
HUGO_
To access other environment variables, adjust your project configuration. For example, to allow access to the HOME and USER environment variables:
security:
funcs:
getenv:
- ^HUGO_
- ^CI$
- ^USER$
- ^HOME$
[security]
[security.funcs]
getenv = ['^HUGO_', '^CI$', '^USER$', '^HOME$']
{
"security": {
"funcs": {
"getenv": [
"^HUGO_",
"^CI$",
"^USER$",
"^HOME$"
]
}
}
}
For more information see configure security.
Examples
{{ getenv "HOME" }} → /home/victor
{{ getenv "USER" }} → victorYou can pass values when building your project:
MY_VAR1=foo MY_VAR2=bar hugo
OR
export MY_VAR1=foo
export MY_VAR2=bar
hugoAnd then retrieve the values within a template:
{{ getenv "MY_VAR1" }} → foo
{{ getenv "MY_VAR2" }} → barLast updated:
June 18, 2026
:
Merge commit 'c86d9f4aa8a58931f52df6516f10b67c807505fb' (be780afa7)
Improve this page