Environment variables

Contents
Get with status
Defaults
Query length first
Env-driven config

Get with status

Get_environment_variable copies a variable such as APP_MODE into a string. Length reports the true size and status reports problems such as a missing name. Test status before trusting the value.

program env_get implicit none character(len=256) :: val integer :: L, stat call get_environment_variable("APP_MODE", val, length=L, status=stat) if (stat == 0) write(*, "(a)") val(1:L) end program env_get

Defaults

Most settings need a fallback when the variable is absent. Assign the default whenever status is nonzero, then continue as normal. The program then runs both on servers with full config and on plain laptops.

program env_default implicit none character(len=256) :: val integer :: L, stat call get_environment_variable("APP_MODE", val, length=L, status=stat) if (stat /= 0) val = "standard" write(*, "(a)") trim(val) end program env_default

Query length first

Fixed buffers can truncate long values, so query the length first. Call once with only length and status, allocate exactly that size, then call again to fetch the text. Deferred length strings fit any value with no waste.

program env_len implicit none character(len=:), allocatable :: val integer :: L, stat call get_environment_variable("APP_MODE", length=L, status=stat) if (stat == 0) then allocate(character(len=L) :: val) call get_environment_variable("APP_MODE", val) end if end program env_len

Env-driven config

Environment driven config reads numbers and flags at startup with zero files to ship. Parse each value with an internal read and keep a default for every failure path. Document each name in the usage text so operators know what to export.

program env_cfg implicit none character(len=256) :: buf integer :: nx = 100 integer :: L, stat, ios call get_environment_variable("NX", buf, length=L, status=stat) if (stat == 0) read(buf(1:L), *, iostat=ios) nx print *, nx end program env_cfg

Next: error stop

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: