LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-05-2011, 11:53 PM   #1
lynne007
LQ Newbie
 
Registered: Jul 2010
Location: usa
Posts: 10

Rep: Reputation: 0
how to define a string of repeated characters


this seems like it should be a simple thing, but I can't find it. Is there a bash shell command that allows you to create a string of repeated characters? Like a string of 100 '*'?
 
Old 02-06-2011, 12:24 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,492

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Might be crude, but something like the following works:
Code:
awk 'BEGIN{$100=OFS="*";print}'
 
Old 02-06-2011, 01:59 AM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
Another option: you can create a function in your .bashrc file. Something like:
Code:
function repeat(){ set -f; for x in $(seq 1 $2) ; do echo -n $1; done; set +f; }
From that point on, you can use "repeat" like a command. For instance:
Code:
jqpublic@nowhere$ repeat '*' 6
******jqpublic@nowhere$
It doesn't print the last newline in case you want to use it as part of another command. For instance:
Code:
jqpublic@nowhere$ echo "`repeat '*' 6`"
******
jqpublic@nowhere$ ls abc`repeat def 3`
ls: cannot access abcdefdefdef: No such file or directory
You have to be careful about special shell characters like the asterisk. For instance, without the double quotes in the echo "`repeat '*' 6`" example, the shell would have expanded the ****** to match filenames.
 
Old 02-06-2011, 05:35 PM   #4
A.Thyssen
Member
 
Registered: May 2006
Location: Brisbane, Australia
Distribution: linux
Posts: 153

Rep: Reputation: 44
In the repeat function, I would quote the $1 even with the set -f in place.

Otherwise any white space in the string to be repeated will be replaced by a single space!

With quotes the set -f is also then not needed, making it more compatible with non-bash shells.


Code:
function repeat(){
for x=1 in $(seq $2); do
    echo -n "$1"
  done
}
echo "$(repeat '>   ' 4)>"
>   >   >   >   >
The previous definition would not produce multiple spaces!


ASIDE: "seq" command is not available in MacOSX... Arrgghhhh...
 
Old 02-06-2011, 06:24 PM   #5
A.Thyssen
Member
 
Registered: May 2006
Location: Brisbane, Australia
Distribution: linux
Posts: 153

Rep: Reputation: 44
Another solution...

Code:
bar="=================================================="
echo "${bar:0:10}"
==========
This can be used for example for generating a 'progress bar'.
This code only uses BASH builtins (except for the sleep)

Code:
  bar="=================================================="
  barlength=${#bar}
  i=0
  while ((i < 100)); do
    n=$((i*barlength / 100)) 
    printf "\r[%-${barlength}s]" "${bar:0:n}"
    ((i += RANDOM%20))      # i = percentage done (replace this)
    sleep 1
  done
  echo
Of course 'i' should be set from somewhere meaningful.
and you may like to 'compete' the bar at the end.
 
Old 02-08-2011, 08:07 PM   #6
lynne007
LQ Newbie
 
Registered: Jul 2010
Location: usa
Posts: 10

Original Poster
Rep: Reputation: 0
thank you for all the replies.

So, it looks like you do have to "write a little program" to do it. I thought maybe there was some direct way of declaring a repetition in a literal constant (like assembler 100c'*', "*" x 100 in Perl).

I ended up just doing it with a rather crude method (before I read these slicker methods):
a='********************'
b='$a$a$a$a$a'
thanks again.
 
Old 02-09-2011, 03:52 AM   #7
sumeet inani
Member
 
Registered: Oct 2008
Posts: 898
Blog Entries: 26

Rep: Reputation: 49
I am using ubuntu 10.04 & gnome-terminal .

If I want to write a letter or word multiple times in my terminal then I do
Code:
$set -o vi
$#Now you can do simple vi task in following commands . In your case  press 'd' then 'escape' then 'x' then '9p' . You get d repeated 9 times.
This isn't for a command in script.

Last edited by sumeet inani; 02-09-2011 at 03:56 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting the no repeated pairs out of a a string charlitos Programming 2 02-17-2009 02:52 AM
characters count in a string powah Programming 3 07-19-2007 02:03 PM
Repeated characters with Logitech MX 5000 Zoombie Linux - Hardware 5 11-30-2006 09:21 PM
Repeated Characters with KDE Desktop Sharing chipfiev Linux - Software 2 12-14-2005 09:19 PM
find repeated characters in a string mcshen Programming 9 02-02-2004 05:43 PM


All times are GMT -5. The time now is 09:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration