如何将Bash脚本生成的字符串传递给其他命令作为参数?
grun First, let's get the core task done: capturing your script's output and passing it correctly to the grun command. Here are two straightforward ways:
Option 1: Store the String in a Variable (Reusable)
Use command substitution to capture the output of your script into a variable, then pass it to grun with proper quoting to preserve spaces and punctuation:
# Generate the target string and store it target_string=$( arr_size=(small medium large) arr_pkg=(vim) # Replace echo -n with printf for cross-shell reliability printf "pkg %s" "${arr_pkg[0]}" for size in "${arr_size[@]}"; do printf ", size %s" "$size" done ) # Use the variable in your grun command (note the double quotes) grun --file 123.txt --tt="$target_string"
The double quotes around "$target_string" are crucial—they ensure the entire string is passed as a single argument to --tt without splitting on spaces or commas.
Option 2: Inline the Generation (No Variable Needed)
If you don't need to reuse the string elsewhere, you can embed the generation directly into the grun command:
grun --file 123.txt --tt="$( arr_size=(small medium large) arr_pkg=(vim) printf "pkg %s" "${arr_pkg[0]}" for size in "${arr_size[@]}"; do printf ", size %s" "$size" done )"
Your current script works, but we can make it cleaner, more portable, and easier to maintain:
1. Ditch echo -n for printf
echo -n has inconsistent behavior across different shell versions (some don't support the -n flag). printf is standardized and reliable for formatted, newline-free output—this is why we used it in the examples above.
2. Simplify the Package Loop (For Single Package)
Since your arr_pkg only has one element (vim), you can skip the loop entirely and directly reference the first element:
printf "pkg %s" "${arr_pkg[0]}"
If you ever add more packages later, you can adjust this to loop through them and append ", pkg $pkg" for each additional one.
3. Build the String Incrementally (Readable)
Instead of printing piecemeal, build the string step by step in a variable—this makes debugging and modifications easier:
arr_size=(small medium large) arr_pkg=(vim) # Start with the package portion result="pkg ${arr_pkg[0]}" # Append each size for size in "${arr_size[@]}"; do result="$result, size $size" done # Pass to grun grun --file 123.txt --tt="$result"
4. Array Joining (Bash 4+)
If you're using Bash 4 or newer, you can join the size array without a loop for a more concise approach:
arr_size=(small medium large) arr_pkg=(vim) # Join size elements with ", size " IFS=", size " size_part="${arr_size[*]}" unset IFS # Build full string full_string="pkg ${arr_pkg[0]}, size $size_part" grun --file 123.txt --tt="$full_string"
This works because setting IFS (Internal Field Separator) to ", size " tells Bash to join the array elements with that string when using ${arr_size[*]}.
Final Tip
Always test with edge cases (like empty arrays, multiple packages) if you plan to expand this script later. And remember: double quotes around variables are your friend when you need to preserve whitespace and special characters.
内容的提问来源于stack exchange,提问作者mebb

