如何在shell中重复破折号(连字符) [英] How to repeat a dash (hyphen) in shell

查看:344
本文介绍了如何在shell中重复破折号(连字符)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在shell中将字符-重复n次?我已经阅读并尝试了,但这不起作用对于-.它将引发错误invalid option.下面是我使用的确切命令:

How can I repeat the character - n times in shell? I have read and tried this, but this does not work for -. It throws error invalid option. Below is the exact command that I used:

printf '-%.0s' {1..100}

原始发布行:printf '-%0.s' {1..100}

我还尝试通过放置\来转义-,但在这种情况下,它会重复\- n次.

I also tried escaping - by putting a \ but in that case it repeats \- n times.

推荐答案

这会引发错误:

$ printf '-%.0s' {1..100}; echo ""
bash: printf: -%: invalid option
printf: usage: printf [-v var] format [arguments]

bash下可以正常工作:

$ printf -- '-%.0s' {1..100}; echo ""
----------------------------------------------------------------------------------------------------

对于其他外壳,请尝试:

For other shells, try:

printf -- '-%.0s' $(seq 100); echo ""

问题是printf期望-启动一个选项.在这种情况下,在Unix/POSIX实用程序中很常见,--printf发出信号,希望不再有其他选择.

The problem was the printf expects that - starts an option. As is common among Unix/POSIX utilities in this type of situation, -- signals to printf to expect no more options.

这篇关于如何在shell中重复破折号(连字符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆