处理 bash 脚本的参数中的空格 [英] Handle whitespaces in arguments to a bash script
本文介绍了处理 bash 脚本的参数中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在处理 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.
这篇关于处理 bash 脚本的参数中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文