处理 bash 脚本的参数中的空格空格、脚本、参数、bash

2023-09-06 18:24:04 作者:官方认证xx

我在处理 bash 脚本中包含空格的参数时遇到问题.

I am having issues handling arguments that contain white spaces in a my bash script.

脚本

#!/bin/bash
for i in $*
do
    echo "$i"
done

调用(带有 2 个参数)

$ ./script.sh "a b" "c"

实际输出(好像有 3 个参数)

a
b
c

预期的输出(好像有 2 个参数)

a b
c

有人可以解释如何获得预期的输出吗?

Can someone explain how to get the expected output?

推荐答案

将第一行的 $* 改为 "$@".

Change $* to "$@" on the first line.