我有一个文件consC,它是一行空格和 *,看起来像这样:
**** ** *
如何读入一个保持*位置完整而不丢失空格的字符串,然后获取*的索引
echo ' ** ** ***** * ' > consC.txt
consC="$(cat consC.txt)"
echo "$consC"
Edit: One of the comments mentions that the second line can be simplified:
consC=$(< consC.txt)
cat
as <
will do the job, $(...)
construct in an assignmentAlthough double-quotes are definitely needed in line 3: echo
.
您还可以设置输入字段分隔符:
while IFS= read line; do
echo "$line"
done < input