简体   繁体   English

在bash中重复printf格式

[英]repeating a printf format in bash

I am trying to printf an array in bash, whose number of element is previously unknown. 我正在尝试在bash中打印一个数组,该数组的元素数量以前未知。 so, the goal is to have something like: 因此,目标是要具有以下内容:

printf  "latc= ${#latc[@]}%s\n" ${latc[@]}

but it does not seem to be possible. 但似乎不可能。 I even tried solution of this thread as 我什至尝试解决这个线程

for x in "${latc[@]}"
do
  printf " %s:%s\n" ${x}
done

, but I am not getting what I want. ,但我没有得到想要的东西。

currently I am working with: 目前,我正在与:

printf  "       latc="
echo ${latc[@]}

clearly not an elegant method of doing thigs. 显然,这不是一种优雅的方法来进行窃贼。 Any help please? 有什么帮助吗?

It looks like you want 看起来像你想要的

printf "latc=%s\n" "${latc[*]}"

Quoting ${latc[*]} produces a single string in which the elements of latc are joined using the first character of IFS (by default, a space). 引用${latc[*]}会生成一个字符串,其中latc的元素使用IFS的第一个字符(默认为空格)进行连接。 Quoting ${latc[@]} induces a special expansion that produces one word for each element of the array. 引用${latc[@]}引起特殊的扩展,该扩展为数组的每个元素产生一个单词。 There is no way to indicate a repeated placeholder in the format string as you seemed to be trying with ${#latc[*]}%s . 您似乎无法尝试使用${#latc[*]}%s来表示格式字符串中的重复占位符。

您可以将数组打印为:

printf "%s\n" "${latc[@]}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM