_VARIABLES.md (2103B)
1 <!-- Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. --> 2 <!-- SPDX-License-Identifier: curl --> 3 # VARIABLES 4 curl supports command line variables (added in 8.3.0). Set variables with 5 --variable name=content or --variable name@file (where `file` can be stdin if 6 set to a single dash (-)). 7 8 Variable contents can be expanded in option parameters using `{{name}}` if the 9 option name is prefixed with `--expand-`. This gets the contents of the 10 variable `name` inserted, or a blank if the name does not exist as a 11 variable. Insert `{{` verbatim in the string by prefixing it with a backslash, 12 like `\{{`. 13 14 You access and expand environment variables by first importing them. You 15 select to either require the environment variable to be set or you can provide 16 a default value in case it is not already set. Plain `--variable %name` 17 imports the variable called `name` but exits with an error if that environment 18 variable is not already set. To provide a default value if it is not set, use 19 `--variable %name=content` or `--variable %name@content`. 20 21 Example. Get the USER environment variable into the URL, fail if USER is not 22 set: 23 24 --variable '%USER' 25 --expand-url = "https://example.com/api/{{USER}}/method" 26 27 When expanding variables, curl supports a set of functions that can make the 28 variable contents more convenient to use. It can trim leading and trailing 29 white space with `trim`, it can output the contents as a JSON quoted string 30 with `json`, URL encode the string with `url`, base64 encode it with `b64` and 31 base64 decode it with `64dec`. To apply functions to a variable expansion, add 32 them colon separated to the right side of the variable. Variable content 33 holding null bytes that are not encoded when expanded causes an error. 34 35 Example: get the contents of a file called $HOME/.secret into a variable 36 called "fix". Make sure that the content is trimmed and percent-encoded when 37 sent as POST data: 38 39 --variable %HOME 40 --expand-variable fix@{{HOME}}/.secret 41 --expand-data "{{fix:trim:url}}" 42 https://example.com/ 43 44 Command line variables and expansions were added in 8.3.0.