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