LinuxQuestions.org
Help answer threads with 0 replies.
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 06-16-2015, 08:12 AM   #1
DoME69
Member
 
Registered: Jan 2008
Posts: 189

Rep: Reputation: 16
How can I echo " " depend on variable?


I'm using bash and I wanted to know how can I echo " " depend on variable?

For example:
Quote:
echo -e "\r[kuku $space]"
$space can be 1 or more space

Result will be depend on $space

Quote:
[kuku ]
[kuku . ]
[kuku .. ]
[kuku ... ]
[kuku .... ]...
* dots above should replaced by " " (space)

Last edited by DoME69; 06-16-2015 at 08:13 AM.
 
Old 06-16-2015, 08:18 AM   #2
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
That could be done like this:

Code:
space="       "
echo -e "\r[kuku ${space}]"
 
Old 06-16-2015, 08:22 AM   #3
genss
Member
 
Registered: Nov 2013
Posts: 741

Rep: Reputation: Disabled
Code:
space="$space "
?



Edit:
Why does this butcher my caps ?
i want to use all caps for shell variables, forum

Last edited by genss; 06-16-2015 at 08:26 AM.
 
Old 06-16-2015, 08:59 AM   #4
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by genss View Post
Code:
space="$space "
?
I never noticed, but
Code:
SPACE="SPACE "
was being converted to lower case.
Code:
[noparse]CAPS[/noparse]
preserves the case, but
Code:
[code]CAPS[/code]
didn't. Weird?

and after several previews, it seems to work?
More Coffee!

Last edited by Habitual; 06-16-2015 at 09:07 AM.
 
Old 06-16-2015, 09:47 AM   #5
genss
Member
 
Registered: Nov 2013
Posts: 741

Rep: Reputation: Disabled
@Habitual
i don't remember the forum doing this before
maybe its just me remembering wrong

edit: now it didn't convert them in CODE tags, or this line
im confused

anyway


@OP
Code:
#!/bin/bash
SPACE=""
for i in {1..5}
do
 echo "[kuku$SPACE]"
 SPACE="$SPACE "
done
something like this ?

Last edited by genss; 06-16-2015 at 09:49 AM.
 
1 members found this post helpful.
Old 06-16-2015, 10:58 AM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,774

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Or, just set Space to the longest string of spaces you expect to need, and then echo an appropriate substring:
Code:
Space="          "
for i in {1..5}; do
    echo "[kuku${Space:0:$i}]"
done
BTW, because of possible conflict with environment variable names, using all upper case for variables in non-system scripts is not recommended.
 
1 members found this post helpful.
Old 06-17-2015, 02:14 AM   #7
DoME69
Member
 
Registered: Jan 2008
Posts: 189

Original Poster
Rep: Reputation: 16
Thanks
 
Old 06-17-2015, 04:01 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
I don't really get your question, but perhaps it is about padding. Use printf:
Code:
$ printf '[%*s]\n[%-*s]\n' 12 "kuku" 12 "kuku"
[        kuku]
[kuku        ]
 
  


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
error regarding to "make depend" command maitanaji Linux - Newbie 2 07-07-2014 08:30 AM
A question about "echo "$CALLER" | tr -s '[:upper:]' '[:lower:]'" thomas2004ch Linux - Software 5 03-13-2012 02:57 PM
How to get the "data type" of an "unknown variable" in "C Language" ? Affair Programming 8 06-20-2009 12:30 PM
BASH: How to NOT echo to screen with "if echo $x | grep ".*"; then" eur0dad Programming 9 07-27-2006 02:14 PM

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

All times are GMT -5. The time now is 12:26 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