On this page
A URL with query parameters can break a terminal command when the shell interprets characters such as & before the HTTP client receives them. To use a URL with query parameters in a shell, quote the entire URL; separately, percent-encode characters that need to be part of a parameter value.
The examples use example.com as a placeholder. Replace it with an endpoint you are authorized to test; the example path is not guaranteed to exist. Bash and PowerShell documentation was not available for verification in this environment, and these commands were not executed here.
Key Takeaways
- Wrap the complete URL in single quotes in both Bash and PowerShell when it is a literal string.
- Shell quoting protects the command from shell interpretation. It does not encode query parameter values.
- Encode a literal plus sign as
%2Bwhen using query parameters that may be decoded as form data. - Avoid putting tokens and personal data in URLs or commands that may be saved or shared.
Why a URL needs shell quoting
Consider a URL with two parameters:
https://example.com/search?q=C%2B%2B&sort=newest
The & separates the query parameters in the URL. But if you paste the URL unquoted into a command, the shell sees that character before the HTTP client does. In Bash, & can put the preceding command in the background and cause the remaining text to be interpreted separately. In PowerShell, & also has special meaning and an unquoted URL may produce a parsing error.
Quoting tells the shell to pass the URL as one argument rather than treating the & as shell syntax.
Advertisement
Quote the URL in Bash
In Bash, pass the URL in single quotes:
curl -- 'https://example.com/search?q=C%2B%2B&sort=newest'
The -- marks the end of command options; the quoted URL follows as an argument. Single quotes also prevent Bash from expanding variables or interpreting other shell metacharacters inside the URL.
For a literal URL, single quotes are usually simpler than double quotes. Double quotes protect &, but still allow expansions such as $variable. If you need to construct a URL from variables, quote the resulting expansion and encode the individual parameter values first; quoting alone does not encode them.
Quote the URL in PowerShell
On Windows, using curl.exe explicitly avoids ambiguity with a PowerShell command or alias named curl:
curl.exe -- 'https://example.com/search?q=C%2B%2B&sort=newest'
This assumes curl.exe is installed and available on the command path. PowerShell single-quoted strings are literal: variables inside them are not expanded. If a literal single quote is needed inside a PowerShell single-quoted string, represent it as two single quotes.
Shell quoting vs. percent-encoding
These are different operations at different boundaries:
| Operation | Purpose | Example |
|---|---|---|
| Shell quoting | Pass the URL as one argument without shell interpretation | 'https://example.com/search?q=C%2B%2B&sort=newest' |
| Percent-encoding | Represent data within a URL component | C++ becomes C%2B%2B as a query value |
The quotes are shell syntax; they are not sent as part of the URL. The %2B sequences are part of the URL passed to the HTTP client.
A literal + in a query string is not universally interpreted the same way by every application. In form-style query decoding, + represents a space, so q=C++ may be read as C followed by two spaces. Encoding each literal plus as %2B avoids that ambiguity for a server using that decoding convention. The server ultimately controls how it interprets its query parameters.
Don't percent-encode the complete assembled URL indiscriminately: characters such as ?, &, and = may be serving as structural separators. Encode each parameter value, then assemble and quote the URL.
Let curl encode parameter values
When constructing a query from separate values, curl can encode them. In Bash:
curl -G \
--data-urlencode 'q=C++' \
--data-urlencode 'sort=newest' \
-- 'https://example.com/search'
Here -G puts the data in the URL query, and --data-urlencode encodes each supplied value. The expected request has two query parameters, with the plus signs in C++ encoded. Use raw values with this approach; passing an already encoded value can encode the percent signs again.
Advertisement
Keep sensitive values out of shared commands
A quoted URL is still visible text. If it contains a token or personal data, it can be recorded in shell history, terminal recordings, screenshots, copied support tickets, CI logs, or HTTP client diagnostics. URLs may also be logged by servers and intermediaries.
- Use a dummy value in examples, screenshots, and support requests. Redact existing captures before sharing them.
- Prefer the service's supported authentication method rather than putting credentials in a query string. A token in a command-line header can still be exposed through history, logs, or process inspection.
- For real secrets, use an approved secret-management mechanism and the client's supported secure input method. Check your terminal, shell history, and logging configuration; disabling history alone does not protect every exposure path.
- If a real token has been exposed, follow your organization's incident procedure and revoke or rotate it as appropriate.
Common Mistakes
- Leaving the URL unquoted: the shell can interpret
&instead of passing it to the client. - Assuming quotes encode data:
'q=C++'still contains literal plus signs; the server may decode them as spaces. - Encoding the entire URL: this can turn query separators into data and change the URL's meaning.
- Encoding a value twice:
%2Bcan become%252B, changing what the server receives after decoding. - Copying a real token into a ticket: redact the command and any resulting logs, not just the visible URL in the first screenshot.
Best Practices
Start with separate, unencoded parameter values. Encode each value once, assemble the query, and quote the complete URL for the shell. Check the receiving application's rules if spaces, plus signs, or repeated parameters matter. For troubleshooting, use non-sensitive sample values and confirm the request on an endpoint you control or are authorized to test.
Conclusion
To pass a URL with query parameters safely to a shell command, quote the entire URL and encode the parameter values separately. These steps solve different problems: one protects the command from shell interpretation, and the other preserves the intended URL data.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.