LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-17-2013, 09:23 PM   #1
ip_address
Member
 
Registered: Apr 2012
Distribution: RedHat
Posts: 42

Rep: Reputation: 2
using printf with a variable


I want to control the number of times a string (in this case "hi") could be printed using a variable (x). How it could be achieved?

For example, I would like to print "hi" 5 times for now but i want to control the number of times "hi" could be printed using a variable.

Code:
x=5

printf "hi\n%.0s" {1.."$x"}
I want result to be like this:

hi
hi
hi
hi
hi
 
Old 03-17-2013, 10:27 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
This code ...
Code:
num=5
yes "hi" |head -n$num
... produces this result ...
Code:
hi
hi
hi
hi
hi
There are many ways to achieve this.

Daniel B. Martin
 
Old 03-17-2013, 10:40 PM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
This code ...
Code:
n=5
awk -v n=$n 'BEGIN{for (i=1;i<=n;i++) print "hi"}'
... produces this result ...
Code:
hi
hi
hi
hi
hi
There are many ways to achieve this.

Daniel B. Martin
 
Old 03-17-2013, 11:03 PM   #4
cfajohnson
LQ Newbie
 
Registered: Aug 2012
Distribution: Linux Mint 17
Posts: 22

Rep: Reputation: Disabled
Code:
repeat()
{
 local num=$1
 shift
 while [ $(( num -= 1 )) -ge 0 ]
 do
   eval "$@"
 done
}
Example:

Code:
$ n=5
$ repeat "$n" 'echo hi'
hi
hi
hi
hi
hi
 
Old 03-18-2013, 02:25 AM   #5
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by ip_address View Post
I want to control the number of times a string (in this case "hi") could be printed using a variable (x). How it could be achieved?

For example, I would like to print "hi" 5 times for now but i want to control the number of times "hi" could be printed using a variable.

Code:
x=5

printf "hi\n%.0s" {1.."$x"}
I want result to be like this:

hi
hi
hi
hi
hi
The problem with your code is that in bash, brace expansion happens before variable substitution and bash
therefore the braces containing a variable cannot be properly expanded. You can use seq instead, though:

Code:
printf "hi\n%.0s" $(seq 1.."$x")
 
Old 03-18-2013, 07:22 AM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by millgates View Post
Code:
printf "hi\n%.0s" $(seq 1.."$x")
I tried this in a bash shell with this unexpected result...
Code:
seq: invalid floating point argument: 1..
Try `seq --help' for more information.
hi
Daniel B. Martin
 
Old 03-18-2013, 08:07 AM   #7
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by danielbmartin View Post
I tried this in a bash shell with this unexpected result...
Code:
seq: invalid floating point argument: 1..
Try `seq --help' for more information.
hi
Daniel B. Martin
Oops... those dots are not supposed to be there, of course. Sorry.

Code:
printf "hi\n%.0s" $(seq 1 "$x")
 
1 members found this post helpful.
Old 03-18-2013, 08:16 AM   #8
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
or
Code:
x=5

for((i=0;i<$x;i++))
do
   printf "Hi\n"
done
 
Old 03-18-2013, 09:52 PM   #9
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by millgates View Post
Code:
printf "hi\n%.0s" $(seq 1 "$x")
I had better luck with this ...
Code:
x=5
printf "hi\n%.0s" $(seq "$x")
Daniel B. Martin
 
Old 03-20-2013, 05:26 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
As millgates said, brace expansion with a variable doesn't work because the braces are expanded before the variable.

Bash Pitfall #33
brace expansion

You're generally much better off using a loop of some kind, as posted above. Although here's another technique you can use with only bash built-ins:

Code:
x=5
printf -v repvar '%0*d' "$x"
printf '%b' "${repvar//0/hi\n}"
The first printf generates a string of zeros equal in length to the input number. The '*' in the format string takes the first (and only) argument as the padding number to use, instead of printing it directly. printf always prints at least once, so no other arguments are needed. '-v' captures the result in a variable instead of printing it directly.

Then a global replacement parameter substitution is used to replace each zero with the desired string. The '%b' format token in the second printf expands backslash escapes like '\n' in the input (making it equivalent to 'echo -en'), thus giving you a newline after each 'hi'.
 
  


Reply



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
[SOLVED] ksh read variable from text file and populate with content of shell variable WindozBytes Programming 4 09-17-2012 01:48 PM
Getting a variable from printf mp85 Linux - Newbie 5 01-31-2012 09:28 AM
Need help with script writing: Storing cmd in variable, print variable, then exe cmds Arodef Programming 3 01-17-2012 12:26 AM
problem while comparing awk field variable with input variable entered using keyboard vinay007 Programming 12 08-23-2011 12:44 AM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:46 PM.

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
Open Source Consulting | Domain Registration