LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How can I echo " " depend on variable? (https://www.linuxquestions.org/questions/programming-9/how-can-i-echo-depend-on-variable-4175545508/)

DoME69 06-16-2015 08:12 AM

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)

veerain 06-16-2015 08:18 AM

That could be done like this:

Code:

space="      "
echo -e "\r[kuku ${space}]"


genss 06-16-2015 08:22 AM

Code:

space="$space "
?



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

Habitual 06-16-2015 08:59 AM

Quote:

Originally Posted by genss (Post 5378023)
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!

genss 06-16-2015 09:47 AM

@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 ?

rknichols 06-16-2015 10:58 AM

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.

DoME69 06-17-2015 02:14 AM

Thanks :)

NevemTeve 06-17-2015 04:01 AM

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        ]



All times are GMT -5. The time now is 04:01 PM.