How to Use a URL with Query Parameters in a Shell Command

Learn to quote URLs with query parameters in Bash and PowerShell, distinguish URL encoding from shell quoting, and inspect requests safely.

Published
Reading time
6 min read
How to Use a URL with Query Parameters in a Shell Command
On this page

A URL with query parameters in a shell command can be misinterpreted before the HTTP client ever sees it. In Bash, an unquoted & separates commands, while ? can participate in filename matching. The safest starting point is to quote the whole URL, then check the request your client actually sends.

This tutorial uses harmless example URLs and covers Bash and PowerShell separately. The examples were not executed, and the official Bash and PowerShell documentation could not be consulted in this environment; check your installed versions' documentation if behavior differs.

Key Takeaways

  • Quote the entire URL as one shell argument; do not rely on URL encoding to protect shell syntax.
  • In Bash, an unquoted & can background the preceding command, and ? can act as a filename wildcard.
  • In PowerShell, single-quoted strings preserve literal URL text; on Windows, use curl.exe to explicitly invoke the curl executable.
  • Use curl's verbose mode to inspect the request target, but treat the resulting logs as potentially sensitive.
  • The examples use example.com as a harmless host, not as a promise that the illustrated path exists.

Why an unquoted URL can break a command

Consider this Bash command, which prints an argument rather than making a network request:

printf '<%s>\n' https://example.com/check?year=2026&status=ready

Bash interprets the & as a shell operator. The printf command before it runs in the background, while status=ready is parsed as a shell assignment rather than as part of the URL. The ? is also eligible for filename matching, although it may remain unchanged if no matching path exists.

Quote the complete URL instead:

printf '<%s>\n' 'https://example.com/check?year=2026&status=ready'

Now the URL is passed to printf as one argument. This is a useful first check, but printing an argument does not prove what an HTTP client subsequently requests.

Advertisement

Run a URL check in Bash

With curl installed, use single quotes around a literal URL:

curl -q --globoff --verbose --output /dev/null -- 'https://example.com/check?year=2026&status=ready'

Here, -q disables curl's default configuration file when placed first, --globoff disables curl's own URL globbing, and -- marks the end of options. Shell quoting and curl globbing are separate concerns: quoting protects the argument from Bash, while --globoff prevents curl from interpreting its own brace and bracket patterns.

--output /dev/null discards the response body; verbose diagnostics still appear in the terminal. A response such as an HTTP error can mean that the request reached the host but the example path does not exist. This command does not follow redirects automatically.

For a URL stored in a variable, use double quotes around the expansion:

url='https://example.com/check?year=2026&status=ready'
printf 'Argument: <%s>\n' "$url"
curl -q --globoff --verbose --output /dev/null -- "$url"

Without the quotes around "$url", Bash can split the expanded value into multiple arguments or apply filename matching. Literal single quotes are convenient for fixed URLs; double quotes are needed here to expand the variable while preserving it as one argument.

Run a URL check in PowerShell

On Windows, with curl available as curl.exe, use:

curl.exe -q --globoff --verbose --output NUL -- 'https://example.com/check?year=2026&status=ready'

The .exe suffix explicitly selects the native curl executable instead of a possible PowerShell alias or function named curl. NUL is the Windows output sink. Single quotes make this a literal PowerShell string, so characters such as & are not interpreted as PowerShell syntax.

For a variable, pass the value as an argument:

$url = 'https://example.com/check?year=2026&status=ready'
Write-Output $url
curl.exe -q --globoff --verbose --output NUL -- $url

Write-Output shows the stored string, not proof of the HTTP request. If you use a different operating system or PowerShell environment, select the installed curl executable and the appropriate output sink. Native-command argument handling can vary by PowerShell version, so inspect the request when troubleshooting.

In PowerShell, double-quoted strings can expand variables and interpret escape sequences. Prefer single quotes for a fixed URL. If the literal string itself contains a single quote, represent it inside a single-quoted PowerShell string by doubling it.

Shell quoting is not URL percent-encoding

Shell quoting controls how the command line becomes arguments. Percent-encoding represents data within a URL. You may need both.

For example, if the intended value of the query parameter is R&D review, encode the value's ampersand and space, then quote the complete URL:

curl -q --globoff --verbose --output /dev/null -- 'https://example.com/search?query=R%26D%20review'

The equivalent Windows PowerShell command is:

curl.exe -q --globoff --verbose --output NUL -- 'https://example.com/search?query=R%26D%20review'

Encoding & as %26 keeps it in the parameter value rather than presenting it as a query-parameter separator. The outer quotes protect the resulting URL from the shell. Encode individual data values as appropriate for the receiving application; don't blindly encode the entire URL or re-encode an already encoded value. Query conventions can differ, especially for + and spaces, so check the receiving application's rules.

Advertisement

Verify what the client requests

Use a benign endpoint you control when possible, and inspect both sides of the request:

  1. Print the URL argument as shown above to catch a quoting or variable-construction error.
  2. Run curl with --verbose. Inspect the request target and host information in its diagnostics. Depending on the HTTP version and proxy, these may appear as a request line or as fields such as :path and :authority.
  3. If you control the receiving service, compare its request logs with the intended path and query. Server-side parsing may decode or transform parameter values after receipt.
  4. Check the status and any Location header. These examples do not follow redirects; if you enable redirect following, inspect each request rather than assuming the first URL is the final destination.

A URL fragment beginning with # is not sent as part of an HTTP request target. Also, client diagnostics alone do not prove which application ultimately processed a request through every proxy or intermediary.

Verbose logs can include query strings, headers, proxy details, or credentials. Use test values, restrict access to logs, and remove sensitive information before sharing them. Avoid putting secrets in URLs where possible: shell history, process listings, logs, and intermediaries may expose them.

Common Mistakes

  • Leaving a URL unquoted: In Bash, & can split the command and ? can participate in filename matching.
  • Encoding instead of quoting: %26 can represent an ampersand in a parameter value, but it is not a general shell-safety mechanism.
  • Encoding query separators as data: Encoding the separator between parameters changes the URL's meaning. Encode the values that need it, not the whole URL indiscriminately.
  • Assuming curl means the same command everywhere: In PowerShell, explicitly selecting curl.exe on Windows avoids ambiguity with aliases or functions.
  • Treating printed text as proof of a request: Inspect curl's diagnostics and, when available, the receiving service's logs.

Best Practices for Runbook Checks

  • Use a known, authorized endpoint and non-sensitive test parameters.
  • Quote complete literal URLs and variable expansions according to the shell in use.
  • Record the shell, operating system, curl availability, and expected status or response in the runbook. Confirm those expectations with the service owner.
  • Never paste an unknown URL into a command that downloads and executes a script. Inspect the destination and content through an approved process first.
  • Do not attach credentials or authorization headers to an unfamiliar destination. Treat redirects and verbose output as potential disclosure paths.

Conclusion

For reliable URL checks, first protect the URL as one shell argument, then separately encode any query data that needs encoding. Verify the request with client diagnostics and, where possible, logs from an endpoint you control.

What would your domain sell for?

A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.