54

I need to generate a string of dots (.characters) as a variable.

I.e., in my Bash script, for input 15 I need to generate this string of length 15: ...............

I need to do so variably. I tried using this as a base (from Unix.com):

for i in {1..100};do printf "%s" "#";done;printf "\n"

But how do I get the 100 to be a variable?

5

9 Answers 9

86

You can get as many NULL bytes as you want from /dev/zero. You can then turn these into other characters. The following prints 16 lowercase a's

head -c 16 < /dev/zero | tr '\0' '\141'
6
  • 10
    +1 for a sweet and portable solution without filthy bashisms or hundreds of processes being spawned
    – fstd
    May 6, 2014 at 2:08
  • 7
    you can use /dev/zero directly, no need to redirect
    – edoceo
    Jul 20, 2015 at 5:41
  • 3
    Not that sweet and portable: "head: illegal option -- c" (SunOS 5.10 Generic_150401-20 i86pc i386 i86pc)
    – dokaspar
    Nov 6, 2015 at 8:21
  • 2
    head -c is portable only on GNU and BSD utils, it's not POSIX.
    – tricasse
    Jan 26, 2016 at 19:40
  • 3
    You can use dd instead with dd if=/dev/zero bs=1 count=16 replacind the head part.
    – NeonMan
    May 31, 2019 at 9:50
44
len=100 ch='#'
printf '%*s' "$len" | tr ' ' "$ch"
4
  • 1
    This is the shortest POSIX 7 solution I have seen so far. Apr 10, 2014 at 11:15
  • 1
    can be even shorter, take out the quotation in "$len" to be $len ;) Jan 31, 2017 at 15:47
  • 1
    shellcheck says you are risking code injection without the quotes on "$len". Feb 15, 2022 at 21:41
  • @JesseChisholm using shellcheck you are risking corporate code into someone else's hands ;)
    – Ricky Levi
    Apr 19 at 10:41
28

Easiest and shortest way without a loop

VAR=15

Prints as many dots as VAR says (change the first dot to any other character if you like):

printf '.%.0s' {1..$VAR}

Saves the dotted line in a variable to be used later:

line=`printf '.%.0s' {1..$VAR}`
echo "Sign here $line"

-Blatantly stolen from dogbane's answer https://stackoverflow.com/a/5349842/3319298

Edit: Since I have now switched to fish shell, here is a function defined in config.fish that does this with convenience in that shell:

function line -a char -a length
  printf '%*s\n' $length "" | tr ' ' $char
end

Usage: line = 8 produces ========, line \" 8 produces """""""".

8
  • 2
    what an interesting way to exploit printf's shell feature that repeatibly consumes arguments until satisfied in combination with a zero-field width, forcing just the string before the % to be printed. This could be used as a trick to divide the count as well (adding the %.0s twice to force it to consume 2 arguments per pass) Jun 22, 2014 at 18:42
  • oh yeah and I forgot something important, since printf processes %s with a null if no args are given, a value of zero would be a problem, for that you would have to add an if statement Jun 22, 2014 at 21:02
  • 3
    This only works for me in my shell (bash) if I use eval: VAR=15 ; eval printf '=%.0s' {1..$VAR}
    – 6EQUJ5
    Sep 27, 2014 at 6:42
  • 6
    @6EQUJ5 In bash, you can't use variables in a brace expansion. Instead, you can use $(seq 1 $VAR) in place of {1..$VAR}
    – wisbucky
    Dec 8, 2017 at 23:56
  • 1
    @6EQUJ5 I meant without using eval. eval is not safe to use in a script. stackoverflow.com/questions/17529220/…
    – wisbucky
    Dec 11, 2017 at 18:32
9

On most systems, you could get away with a simple

N=100
myvar=`perl -e "print '.' x $N;"`
6

I demonstrated a way to accomplish this task with a single command in another question, assuming it's a fixed number of characters to be produced.

I added an addendum to the end about producing a variable number of repeated characters, which is what you asked for, so my previous answer is relevant here:

https://stackoverflow.com/a/17030976/2284005

I provided a full explanation of how it works there. Here I'll just add the code to accomplish what you're asking for:

    n=20 # This the number of characters you want to produce

    variable=$(printf "%0.s." $(seq 1 $n)) # Fill $variable with $n periods

    echo $variable # Output content of $variable to terminal

Outputs:

....................
1
  • n=0 fails to produce the correct number of characters. As do most of the printf solutions.
    – NOYB
    Sep 7, 2020 at 19:19
4

The solution without loops:

N=100
myvar=`seq 1 $N | sed 's/.*/./' | tr -d '\n'`
4

You can use C-style for loops in Bash:

num=100
string=$(for ((i=1; i<=$num; i++));do printf "%s" "#";done;printf "\n")

Or without a loop, using printf without using any externals such as sed or tr:

num=100
printf -v string "%*s" $num ' ' '' $'\n'
string=${string// /#}
2
num=100
myvar=$(jot -b . -s '' $num)
echo $myvar
-1

When I have to create a string that contains $x repetitions of a known character with $x below a constant value, I use this idiom:

base='....................'
# 0 <= $x <= ${#base}
x=5
expr "x$base" : "x\(.\{$x\}\)"    # Will output '\n' too

Output:

.....
1
  • 2
    In bash and ksh (perhaps zsh), you could write echo ${base:0:$x} -- details here Jul 22, 2014 at 13:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.