巴什参数扩展符 [英] Bash parameter expansion delimiter

查看:94
本文介绍了巴什参数扩展符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让 1:2:3:4:5:6:7:8:9:10 使用参数扩展 {1..10} 和模式匹配:

I'm trying to get 1:2:3:4:5:6:7:8:9:10 using parameter expansion {1..10} and pattern matching:

$ var=$(echo {1..10})
$ echo ${var// /:}
1:2:3:4:5:6:7:8:9:10

有没有更优雅的方式(单行),这样做呢?

Is there a more elegant way (one-liner) to do this?

推荐答案

与@ choroba的约优雅意见达成一致,这里有一些其他的beholdables:

Agreeing with @choroba's comment about elegance, here are some other beholdables:

# seq is a gnu core utility
seq 1 10 | paste -sd:
# Or:
seq -s: 1 10

# {1..10} is bash-specific
printf "%d\n" {1..10} | paste -sd:

# posix compliant
yes | head -n10 | grep -n . | cut -d: -f1 | paste -sd:

这篇关于巴什参数扩展符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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