variable.md (3506B)
1 --- 2 c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 3 SPDX-License-Identifier: curl 4 Long: variable 5 Arg: <[%]name=text/@file> 6 Help: Set variable 7 Category: curl 8 Added: 8.3.0 9 Multi: append 10 See-also: 11 - config 12 Example: 13 - --variable name=smith --expand-url "$URL/{{name}}" 14 --- 15 16 # `--variable` 17 18 Set a variable with `name=content` or `name@file` (where `file` can be stdin 19 if set to a single dash (`-`)). The name is a case sensitive identifier that 20 must consist of no other letters than a-z, A-Z, 0-9 or underscore. The 21 specified content is then associated with this identifier. 22 23 Setting the same variable name again overwrites the old contents with the new. 24 25 The contents of a variable can be referenced in a later command line option 26 when that option name is prefixed with `--expand-`, and the name is used as 27 `{{name}}`. 28 29 --variable can import environment variables into the name space. Opt to either 30 require the environment variable to be set or provide a default value for the 31 variable in case it is not already set. 32 33 --variable %name imports the variable called `name` but exits with an error if 34 that environment variable is not already set. To provide a default value if 35 the environment variable is not set, use --variable %name=content or 36 --variable %name@content. Note that on some systems - but not all - 37 environment variables are case insensitive. 38 39 Added in curl 8.12.0: you can get a byte range from the source by appending 40 `[start-end]` to the variable name, where *start* and *end* are byte offsets 41 to include from the contents. For example, asking for offset "2-10" means 42 offset two to offset ten, inclusive, resulting in 9 bytes in total. `2-2` 43 means a single byte at offset 2. Not providing a second number implies to the 44 end of data. The start offset cannot be larger than the end offset. Asking for 45 a range that is outside of the file size makes the variable contents empty. 46 For example, getting the first one hundred bytes from a given file: 47 48 curl --variable "fraction[0-99]@filename" 49 50 Given a byte range that has no data results in an empty string. Asking for a 51 range that is larger than the content makes curl use the piece of the data 52 that exists. 53 54 To assign a variable using contents from another variable, use 55 --expand-variable. Like for example assigning a new variable using contents 56 from two other: 57 58 curl --expand-variable "user={{firstname}} {{lastname}}" 59 60 When expanding variables, curl supports a set of functions that can make the 61 variable contents more convenient to use. You apply a function to a variable 62 expansion by adding a colon and then list the desired functions in a 63 comma-separated list that is evaluated in a left-to-right order. Variable 64 content holding null bytes that are not encoded when expanded causes an 65 error. 66 67 Available functions: 68 69 ## `trim` 70 71 removes all leading and trailing white space. 72 73 Example: 74 75 curl --expand-url https://example.com/{{var:trim}} 76 77 ## `json` 78 79 outputs the content using JSON string quoting rules. 80 81 Example: 82 83 curl --expand-data {{data:json}} https://example.com 84 85 ## `url` 86 87 shows the content URL (percent) encoded. 88 89 Example: 90 91 curl --expand-url https://example.com/{{path:url}} 92 93 ## `b64` 94 95 expands the variable base64 encoded 96 97 Example: 98 99 curl --expand-url https://example.com/{{var:b64}} 100 101 ## `64dec` 102 103 decodes a base64 encoded character sequence. If the sequence is not possible 104 to decode, it instead outputs `[64dec-fail]` 105 106 Example: 107 108 curl --expand-url https://example.com/{{var:64dec}} 109 110 (Added in 8.13.0)