LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 12-09-2022, 12:21 AM   #1
lqmsh
LQ Newbie
 
Registered: Oct 2021
Posts: 29

Rep: Reputation: Disabled
how to print space in for loop bash


I am trying to print space in for loop by the script below

Code:
#!/bin/bash
for i in - - - - - - up to date; do
   echo -n $i
   sleep 0.5
done
the output I want is
------ up to date
but the script output
------uptodate

How can I print spaces where I want?
thanks in advance.
 
Old 12-09-2022, 12:27 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 19,594

Rep: Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629
Quote:
Originally Posted by lqmsh View Post
How can I print spaces where I want?
Put space where you want. In your code you do not print (echo) space at all. Why do you think it will be added automatically?
Code:
echo -n "$i "  # << add a space after $i
 
1 members found this post helpful.
Old 12-09-2022, 08:06 AM   #3
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,077

Rep: Reputation: 2169Reputation: 2169Reputation: 2169Reputation: 2169Reputation: 2169Reputation: 2169Reputation: 2169Reputation: 2169Reputation: 2169Reputation: 2169Reputation: 2169
Quote:
Originally Posted by lqmsh View Post
How can I print spaces where I want?
Read and understand the Quoting and Word Splitting parts of the manual.

 
Old 12-09-2022, 08:48 PM   #4
lqmsh
LQ Newbie
 
Registered: Oct 2021
Posts: 29

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by boughtonp View Post
Read and understand the Quoting and Word Splitting parts of the manual.

Thank you. Solved.
 
Old 12-10-2022, 05:10 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,228

Rep: Reputation: 995Reputation: 995Reputation: 995Reputation: 995Reputation: 995Reputation: 995Reputation: 995Reputation: 995
Code:
for i in - - - - - - " up" " to" " date"
Code:
for i in - - - - - - " up to date"
Code:
for i in - - - - - -
do
...
done
echo " up to date"
 
1 members found this post helpful.
  


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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 04:49 PM
Bash script issue (for loop inside a loop) Mperonen Programming 3 08-08-2013 03:14 AM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 06:00 PM
[SOLVED] [BASH] non-empty variable before loop end, is empty after exiting loop aitor Programming 2 08-26-2010 10:57 AM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 04:50 AM

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

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