Skip to content

Commit

Permalink
Cookbook getenv\n\nRecreate PR #2383
Browse files Browse the repository at this point in the history
  • Loading branch information
Cuihtlauac ALVARADO committed May 13, 2024
1 parent 2f496c7 commit 7be3088
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions data/cookbook/read-environment-variable/00-stdlib.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
packages: []
discussion: |
- **Understanding `Sys.getenv` and `Sys.getenv_opt`:** Both `Sys.getenv` and `Sys.getenv_opt` functions take a environment variable name and return its value. `Sys.getenv` returns directly the value, but raises a `Not_found` exception if the variable doesn't exist. `Sys.getenv_opt` returns an `option` type: `Some value` if the variable exists and `None` if not.
---

let path = Sys.getenv "PATH"
let () =
Printf.printf "The path is %s\n" path

let () =
match Sys.getenv_opt "OPAM_SWITCH_PREFIX" with
| Some p ->
Printf.printf "The Opam switch prefix is %s\n" p
| None ->
print_string "The Opam switch prefix is not set.\n"

0 comments on commit 7be3088

Please sign in to comment.