简体   繁体   中英

Bash parameter expansion delimiter

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?

优雅在旁观者的眼中:

( set {1..10} ; IFS=: ; echo "$*" )

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:

另一种可能性

echo {1..9}: 10 | tr -d ' '

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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