Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating string of repeated characters in shell script [duplicate]

Tags:

shell

unix

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?

like image 834
RubiCon10 Avatar asked Jul 09 '10 10:07

RubiCon10


People also ask

What is ${} in shell script?

${} Parameter Substitution/Expansion A parameter, in Bash, is an entity that is used to store values. A parameter can be referenced by a number, a name, or by a special symbol. When a parameter is referenced by a number, it is called a positional parameter.

What is $1 $2 in shell script?

$0 is the name of the script itself (script.sh) $1 is the first argument (filename1) $2 is the second argument (dir1) $9 is the ninth argument.


2 Answers

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' 
like image 200
Electron Avatar answered Oct 30 '22 16:10

Electron


len=100 ch='#' printf '%*s' "$len" | tr ' ' "$ch" 
like image 28
Chris Johnsen Avatar answered Oct 30 '22 16:10

Chris Johnsen