LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-18-2020, 04:19 PM   #1
eli67
LQ Newbie
 
Registered: Mar 2020
Posts: 6

Rep: Reputation: Disabled
how to display space in a for loop


I have this statement in my code:

for i in $(echo "Hello " $name) ; do echo ${i^} ; done | tr '\n' ' ' ; echo -e "\r" | echo ", bye"

I would like to display space in the terminal, like this:


....Hello Name , bye

(without full stop)


How can I do this?
Thank you for whom will help me.

Last edited by eli67; 03-18-2020 at 04:22 PM.
 
Old 03-18-2020, 05:34 PM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
The meaning of this code is not clear to me, but you could write
Code:
echo " , bye"
EDIT: When I run your unchanged code, I get a space:
Code:
Hello Name , bye

Last edited by berndbausch; 03-18-2020 at 05:36 PM.
 
Old 03-18-2020, 07:17 PM   #3
eli67
LQ Newbie
 
Registered: Mar 2020
Posts: 6

Original Poster
Rep: Reputation: Disabled
I have this statement in my code:

for i in $(echo "Hello " $name) ; do echo ${i^} ; done | tr '\n' ' ' ; echo -e "\r" | echo ", bye"

I would like to display space in the terminal, like this:


(here I would like to live some space, like a tab space)Hello Name , bye

(without full stop)


How can I do this?
Thank you for whom will help me.
 
Old 03-18-2020, 07:58 PM   #4
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
You put echo -n " " or echo -n -e "\t" before the for loop.
 
Old 03-19-2020, 02:40 AM   #5
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
Could you please tell us what do you exactly have in mind with this script? I mean, strictly speaking, just
Code:
echo -e "\tHello $name , bye"
would be enough to fulfill your request. Is that $(echo "Hello " $name) just a placeholder for commands you want to put into the real script? Then what would they be?
 
Old 03-19-2020, 02:49 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,035

Rep: Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344
It looks like a homework for me.
assuming:
Code:
for i in $(some command)...
# you can do:
some command | sed 's/^/<space before>/;s/$/, bye/'
should do it - if I understand well.
 
Old 03-19-2020, 11:06 AM   #7
eli67
LQ Newbie
 
Registered: Mar 2020
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thank you to everybody so far but I still looking for help.

Thank you to pan64. You give me some right tips.Your code worked partially.

I modified the code in this way:


echo -e "\t\tPlease enter your name?"
read name
name="${name,,}"
for i in $(echo "Hello " $name) ; do echo ${i^} ; done | sed 's/^/\t\t/;s/$/, here is your order:/'

If I enter the name like : mike denver


The output is:

<here come out the right space of two tabs>Hello, here is your order:
<here come out the right space of two tabs>Mike, here is your order:
<here come out the right space of two tabs>Denver, here is your order:

Basically, it create the right space from the left side based on the number of tabs that I put (\t), but it repeats the print line for Hello plus for each names entered. I didn't understand how to put sed command outside.
My expected result should be this:

<here come out the right space of two tabs>Hello, Mike Denver here is your order:
 
Old 03-19-2020, 11:34 AM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,781

Rep: Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936
A for loop is a programming statement that allows a section of code to repeat. A bash for loop can iterate over a list or a sequence and in the case of your program it is a list that is created using your command $(echo "Hello" $name). The default separator for a list is a space and if you enter multiple names your loop will be a list like
hello
first name ( mike )
second name ( denver )

And create a line here is your order for each item in the list as posted above and as shown in your output. I assume this is homework and while we should not write the script for you we can try to help with specific problems. I'm not sure your on the right track without seeing the actual assignment.

https://www.cyberciti.biz/faq/bash-for-loop/
 
Old 03-19-2020, 12:08 PM   #9
eli67
LQ Newbie
 
Registered: Mar 2020
Posts: 6

Original Poster
Rep: Reputation: Disabled
Hi MichaelK

Thank you for your email.

Anyway, I would like to explain that if I am doing this research, is because I am still learning Unix and I am asking help, just because I am creating my personal code and I would like to make this change even if it is not necessary but I like to learn, and this is the reason because I asked this community help to sort out my curiosity. Thank you anyway for all of you that gave me some of your precious time.
 
Old 03-20-2020, 01:52 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,035

Rep: Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344
what about this:
Code:
for word in $text
do
    echo "<spaces>$word this is your order"
done
 
Old 03-20-2020, 02:11 AM   #11
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,781

Rep: Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936
At the moment there isn't enough information provided to know why the OP even needs a loop.

Code:
read name
echo "Hello $name here is your order:"
 
Old 03-20-2020, 07:14 AM   #12
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
My take on it
Code:
#!/bin/bash
read -p $'\tPlease enter your name? ' -a name
name=(${name[@],,})
name=(${name[@]^})
echo -e "\t\tHello ${name[@]}, here is your order:"
Example:
Code:
$ bash script
	Please enter your name? ONE two tHREE fOuR
		Hello One Two Three Four, here is your order:
 
Old 03-20-2020, 07:38 AM   #13
eli67
LQ Newbie
 
Registered: Mar 2020
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thumbs up

Hi shruggy Urrahhhhh ....!!!!!!

You are my hero....!!!!

It works perfectly

Thank you so much everybody
 
  


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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
Division of Logical Memory Space in to User Space and Kernel Space shreshtha Linux - Newbie 2 01-14-2010 09:59 AM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
for loop only works properly on first loop symo0009 Programming 1 12-25-2005 05:17 PM
while loop or for loop? mijohnst Programming 18 11-21-2005 04:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:11 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
Open Source Consulting | Domain Registration