如何在Bash及OSX系统的Bash中执行多行命令?
Hey there! Let's walk through how to run multi-line commands in Bash—whether you're on a Linux system or macOS (OSX). I'll also show you how to apply this to your specific curl example.
1. Line Continuation with \
The simplest way to split a command across multiple lines is to add a backslash \ at the end of each line (make sure there's no space after the backslash—that's a common gotcha!). This tells Bash that the command isn't finished yet, and it should continue reading from the next line.
Example with Your curl Command:
curl https://sandbox.checkout.com/api2/v2/customers/cust_8AB7845C-35D6-4BAD-BED0-D0D7F75C50F0/cards \ -H "Authorization: sk_093F4C8D-E608-4B8D-9B39-8C2491345864" \ -H "Content-Type:application/json;charset=UTF-8" \ -X POST \ -d '{ "name" : "Miss Matt Quigley", "number" : "4242424242424242", "expiryMonth" : "06", "expiryYear" : "2018", "cvv" : "100", "billingDetails" : { "addressLine1" : "56 Layla Rue", "addressLine2" : "Junius Plain", "city" : "PortDe..." } }'
Notice that the JSON payload inside the -d flag can also be split across lines for readability—Bash will treat it as a single string as long as the quotes are properly closed.
2. Command Grouping with () or {}
If you want to run a sequence of commands as a single block (and optionally capture their output), you can wrap them in parentheses () or curly braces {}. This works great for multi-line scripts or command chains.
- Parentheses
(): Runs the commands in a subshell. Changes to the environment (likecd) won't affect your current shell session.( cd /tmp echo "Working in: $PWD" touch test.txt ) - Curly Braces
{}: Runs the commands in your current shell. Note that you need a space after{and a semicolon before}.{ echo "Current dir: $PWD" ls -la }
3. Here-Documents (<<EOF) for Multi-Line Input
For commands that accept multi-line input (like cat, awk, or even curl when sending large payloads), a here-document is a clean way to structure your content.
Example with curl Using Here-Doc:
Instead of cramming the JSON into the -d flag directly, you can feed it via standard input:
curl https://sandbox.checkout.com/api2/v2/customers/cust_8AB7845C-35D6-4BAD-BED0-D0D7F75C50F0/cards \ -H "Authorization: sk_093F4C8D-E608-4B8D-9B39-8C2491345864" \ -H "Content-Type:application/json;charset=UTF-8" \ -X POST \ -d @- <<EOF { "name" : "Miss Matt Quigley", "number" : "4242424242424242", "expiryMonth" : "06", "expiryYear" : "2018", "cvv" : "100", "billingDetails" : { "addressLine1" : "56 Layla Rue", "addressLine2" : "Junius Plain", "city" : "PortDe..." } } EOF
The @- tells curl to read the data from standard input, and <<EOF marks the start of the here-document (you can use any word instead of EOF, just make sure it's the same at the end, and it's on its own line with no leading/trailing spaces).
What About OSX Bash?
Good news—all the methods above work exactly the same in OSX's Bash! While macOS now uses Zsh as the default shell, if you're explicitly using Bash (either by running bash in the terminal or setting it as your default), the multi-line command syntax is identical to Linux-based Bash. The only edge case might be very old OSX versions with outdated Bash releases, but even those support the core line continuation and grouping features.
内容的提问来源于stack exchange,提问作者Alex Kokorin

