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?
Bash uses the value formed by expanding the rest of parameter as the new parameter ; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter . This is known as indirect expansion .
bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc.
${var/Pattern/Replacement}First match of Pattern, within var replaced with Replacement. If Replacement is omitted, then the first match of Pattern is replaced by nothing, that is, deleted.
Manipulating and/or expanding variables ${parameter} Same as $parameter, i.e., value of the variable parameter. In certain contexts, only the less ambiguous ${parameter} form works. May be used for concatenating variables with strings.
Elegance is in the eye of the beholder:
( 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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With