curl/docs/cmdline-opts/variable.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

59 lines
1.9 KiB
Markdown
Raw Normal View History

---
tool: add "variable" support Add support for command line variables. Set variables with --variable name=content or --variable name@file (where "file" can be stdin if set to a single dash (-)). Variable content is expanded in option parameters using "{{name}}" (without the quotes) if the option name is prefixed with "--expand-". This gets the contents of the variable "name" inserted, or a blank if the name does not exist as a variable. Insert "{{" verbatim in the string by prefixing it with a backslash, like "\\{{". Import an environment variable with --variable %name. It makes curl exit with an error if the environment variable is not set. It can also rather get a default value if the variable does not exist, using =content or @file like shown above. Example: get the USER environment variable into the URL: --variable %USER --expand-url = "https://example.com/api/{{USER}}/method" When expanding variables, curl supports a set of functions that can make the variable contents more convenient to use. It can trim leading and trailing white space with "trim", output the contents as a JSON quoted string with "json", URL encode it with "url" and base 64 encode it with "b64". To apply functions to a variable expansion, add them colon separated to the right side of the variable. They are then performed in a left to right order. Example: get the contents of a file called $HOME/.secret into a variable called "fix". Make sure that the content is trimmed and percent-encoded sent as POST data: --variable %HOME=/home/default --expand-variable fix@{{HOME}}/.secret --expand-data "{{fix:trim:url}}" https://example.com/ Documented. Many new test cases. Co-brainstormed-by: Emanuele Torre Assisted-by: Jat Satiro Closes #11346
2023-07-31 17:50:28 +08:00
c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
SPDX-License-Identifier: curl
Long: variable
Arg: <[%]name=text/@file>
Help: Set variable
Category: curl
Added: 8.3.0
Multi: append
See-also:
- config
Example:
- --variable name=smith --expand-url "$URL/{{name}}"
tool: add "variable" support Add support for command line variables. Set variables with --variable name=content or --variable name@file (where "file" can be stdin if set to a single dash (-)). Variable content is expanded in option parameters using "{{name}}" (without the quotes) if the option name is prefixed with "--expand-". This gets the contents of the variable "name" inserted, or a blank if the name does not exist as a variable. Insert "{{" verbatim in the string by prefixing it with a backslash, like "\\{{". Import an environment variable with --variable %name. It makes curl exit with an error if the environment variable is not set. It can also rather get a default value if the variable does not exist, using =content or @file like shown above. Example: get the USER environment variable into the URL: --variable %USER --expand-url = "https://example.com/api/{{USER}}/method" When expanding variables, curl supports a set of functions that can make the variable contents more convenient to use. It can trim leading and trailing white space with "trim", output the contents as a JSON quoted string with "json", URL encode it with "url" and base 64 encode it with "b64". To apply functions to a variable expansion, add them colon separated to the right side of the variable. They are then performed in a left to right order. Example: get the contents of a file called $HOME/.secret into a variable called "fix". Make sure that the content is trimmed and percent-encoded sent as POST data: --variable %HOME=/home/default --expand-variable fix@{{HOME}}/.secret --expand-data "{{fix:trim:url}}" https://example.com/ Documented. Many new test cases. Co-brainstormed-by: Emanuele Torre Assisted-by: Jat Satiro Closes #11346
2023-07-31 17:50:28 +08:00
---
# `--variable`
Set a variable with `name=content` or `name@file` (where `file` can be stdin
if set to a single dash (`-`)). The name is a case sensitive identifier that
tool: add "variable" support Add support for command line variables. Set variables with --variable name=content or --variable name@file (where "file" can be stdin if set to a single dash (-)). Variable content is expanded in option parameters using "{{name}}" (without the quotes) if the option name is prefixed with "--expand-". This gets the contents of the variable "name" inserted, or a blank if the name does not exist as a variable. Insert "{{" verbatim in the string by prefixing it with a backslash, like "\\{{". Import an environment variable with --variable %name. It makes curl exit with an error if the environment variable is not set. It can also rather get a default value if the variable does not exist, using =content or @file like shown above. Example: get the USER environment variable into the URL: --variable %USER --expand-url = "https://example.com/api/{{USER}}/method" When expanding variables, curl supports a set of functions that can make the variable contents more convenient to use. It can trim leading and trailing white space with "trim", output the contents as a JSON quoted string with "json", URL encode it with "url" and base 64 encode it with "b64". To apply functions to a variable expansion, add them colon separated to the right side of the variable. They are then performed in a left to right order. Example: get the contents of a file called $HOME/.secret into a variable called "fix". Make sure that the content is trimmed and percent-encoded sent as POST data: --variable %HOME=/home/default --expand-variable fix@{{HOME}}/.secret --expand-data "{{fix:trim:url}}" https://example.com/ Documented. Many new test cases. Co-brainstormed-by: Emanuele Torre Assisted-by: Jat Satiro Closes #11346
2023-07-31 17:50:28 +08:00
must consist of no other letters than a-z, A-Z, 0-9 or underscore. The
specified content is then associated with this identifier.
Setting the same variable name again overwrites the old contents with the new.
tool: add "variable" support Add support for command line variables. Set variables with --variable name=content or --variable name@file (where "file" can be stdin if set to a single dash (-)). Variable content is expanded in option parameters using "{{name}}" (without the quotes) if the option name is prefixed with "--expand-". This gets the contents of the variable "name" inserted, or a blank if the name does not exist as a variable. Insert "{{" verbatim in the string by prefixing it with a backslash, like "\\{{". Import an environment variable with --variable %name. It makes curl exit with an error if the environment variable is not set. It can also rather get a default value if the variable does not exist, using =content or @file like shown above. Example: get the USER environment variable into the URL: --variable %USER --expand-url = "https://example.com/api/{{USER}}/method" When expanding variables, curl supports a set of functions that can make the variable contents more convenient to use. It can trim leading and trailing white space with "trim", output the contents as a JSON quoted string with "json", URL encode it with "url" and base 64 encode it with "b64". To apply functions to a variable expansion, add them colon separated to the right side of the variable. They are then performed in a left to right order. Example: get the contents of a file called $HOME/.secret into a variable called "fix". Make sure that the content is trimmed and percent-encoded sent as POST data: --variable %HOME=/home/default --expand-variable fix@{{HOME}}/.secret --expand-data "{{fix:trim:url}}" https://example.com/ Documented. Many new test cases. Co-brainstormed-by: Emanuele Torre Assisted-by: Jat Satiro Closes #11346
2023-07-31 17:50:28 +08:00
The contents of a variable can be referenced in a later command line option
when that option name is prefixed with `--expand-`, and the name is used as
`{{name}}`.
tool: add "variable" support Add support for command line variables. Set variables with --variable name=content or --variable name@file (where "file" can be stdin if set to a single dash (-)). Variable content is expanded in option parameters using "{{name}}" (without the quotes) if the option name is prefixed with "--expand-". This gets the contents of the variable "name" inserted, or a blank if the name does not exist as a variable. Insert "{{" verbatim in the string by prefixing it with a backslash, like "\\{{". Import an environment variable with --variable %name. It makes curl exit with an error if the environment variable is not set. It can also rather get a default value if the variable does not exist, using =content or @file like shown above. Example: get the USER environment variable into the URL: --variable %USER --expand-url = "https://example.com/api/{{USER}}/method" When expanding variables, curl supports a set of functions that can make the variable contents more convenient to use. It can trim leading and trailing white space with "trim", output the contents as a JSON quoted string with "json", URL encode it with "url" and base 64 encode it with "b64". To apply functions to a variable expansion, add them colon separated to the right side of the variable. They are then performed in a left to right order. Example: get the contents of a file called $HOME/.secret into a variable called "fix". Make sure that the content is trimmed and percent-encoded sent as POST data: --variable %HOME=/home/default --expand-variable fix@{{HOME}}/.secret --expand-data "{{fix:trim:url}}" https://example.com/ Documented. Many new test cases. Co-brainstormed-by: Emanuele Torre Assisted-by: Jat Satiro Closes #11346
2023-07-31 17:50:28 +08:00
--variable can import environment variables into the name space. Opt to either
require the environment variable to be set or provide a default value for the
variable in case it is not already set.
--variable %name imports the variable called `name` but exits with an error if
that environment variable is not already set. To provide a default value if
tool: add "variable" support Add support for command line variables. Set variables with --variable name=content or --variable name@file (where "file" can be stdin if set to a single dash (-)). Variable content is expanded in option parameters using "{{name}}" (without the quotes) if the option name is prefixed with "--expand-". This gets the contents of the variable "name" inserted, or a blank if the name does not exist as a variable. Insert "{{" verbatim in the string by prefixing it with a backslash, like "\\{{". Import an environment variable with --variable %name. It makes curl exit with an error if the environment variable is not set. It can also rather get a default value if the variable does not exist, using =content or @file like shown above. Example: get the USER environment variable into the URL: --variable %USER --expand-url = "https://example.com/api/{{USER}}/method" When expanding variables, curl supports a set of functions that can make the variable contents more convenient to use. It can trim leading and trailing white space with "trim", output the contents as a JSON quoted string with "json", URL encode it with "url" and base 64 encode it with "b64". To apply functions to a variable expansion, add them colon separated to the right side of the variable. They are then performed in a left to right order. Example: get the contents of a file called $HOME/.secret into a variable called "fix". Make sure that the content is trimmed and percent-encoded sent as POST data: --variable %HOME=/home/default --expand-variable fix@{{HOME}}/.secret --expand-data "{{fix:trim:url}}" https://example.com/ Documented. Many new test cases. Co-brainstormed-by: Emanuele Torre Assisted-by: Jat Satiro Closes #11346
2023-07-31 17:50:28 +08:00
the environment variable is not set, use --variable %name=content or
--variable %name@content. Note that on some systems - but not all -
environment variables are case insensitive.
When expanding variables, curl supports a set of functions that can make the
variable contents more convenient to use. You apply a function to a variable
expansion by adding a colon and then list the desired functions in a
comma-separated list that is evaluated in a left-to-right order. Variable
content holding null bytes that are not encoded when expanded, causes an
tool: add "variable" support Add support for command line variables. Set variables with --variable name=content or --variable name@file (where "file" can be stdin if set to a single dash (-)). Variable content is expanded in option parameters using "{{name}}" (without the quotes) if the option name is prefixed with "--expand-". This gets the contents of the variable "name" inserted, or a blank if the name does not exist as a variable. Insert "{{" verbatim in the string by prefixing it with a backslash, like "\\{{". Import an environment variable with --variable %name. It makes curl exit with an error if the environment variable is not set. It can also rather get a default value if the variable does not exist, using =content or @file like shown above. Example: get the USER environment variable into the URL: --variable %USER --expand-url = "https://example.com/api/{{USER}}/method" When expanding variables, curl supports a set of functions that can make the variable contents more convenient to use. It can trim leading and trailing white space with "trim", output the contents as a JSON quoted string with "json", URL encode it with "url" and base 64 encode it with "b64". To apply functions to a variable expansion, add them colon separated to the right side of the variable. They are then performed in a left to right order. Example: get the contents of a file called $HOME/.secret into a variable called "fix". Make sure that the content is trimmed and percent-encoded sent as POST data: --variable %HOME=/home/default --expand-variable fix@{{HOME}}/.secret --expand-data "{{fix:trim:url}}" https://example.com/ Documented. Many new test cases. Co-brainstormed-by: Emanuele Torre Assisted-by: Jat Satiro Closes #11346
2023-07-31 17:50:28 +08:00
error.
Available functions:
## trim
removes all leading and trailing white space.
## json
outputs the content using JSON string quoting rules.
## url
shows the content URL (percent) encoded.
## b64
expands the variable base64 encoded