Loop

Forum rules
Before you post please read how to get help
User avatar
mrmajik45
Level 4
Level 4
Posts: 254
Joined: Tue Aug 29, 2017 9:51 pm
Location: USA, indiana
Contact:

Loop

Postby mrmajik45 » Sat Nov 11, 2017 1:49 pm

Code: Select all

while true; do echo -n "Mrmajik45 "; done

Replace mrmajik45 with whatever you want it to say.
The linux coder.
Image

User avatar
xenopeek
Level 24
Level 24
Posts: 21465
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Loop

Postby xenopeek » Sat Nov 11, 2017 1:52 pm

Let's optimize that! This command does the exact same thing :wink:
yes Mrmajik45

Ctrl+C to interrupt the command.
Image

User avatar
mrmajik45
Level 4
Level 4
Posts: 254
Joined: Tue Aug 29, 2017 9:51 pm
Location: USA, indiana
Contact:

Re: Loop

Postby mrmajik45 » Sat Nov 11, 2017 2:31 pm

That could be the Straight line version.
The linux coder.
Image

User avatar
xenopeek
Level 24
Level 24
Posts: 21465
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Loop

Postby xenopeek » Sat Nov 11, 2017 3:00 pm

Ah, I missed the -n on your echo.

To turn the newlines into spaces we can do something like:
yes Mrmajik45 | tr $'\n' ' '
Or:
yes Mrmajik45 | paste -sd ' '

Not as neat :)
Image

User avatar
Termy
Level 3
Level 3
Posts: 170
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Loop

Postby Termy » Sun Nov 26, 2017 1:18 pm

More random information:

  • : is shorthand for true.
  • [ 1 ] can also be used as true, where 1 is any string.
  • Conversely, [ ] can be used as the false command.
  • The opposite can be done with the until loop.
  • There is also a for loop, and while read construct.
Here to help.

lmuserx4849
Level 4
Level 4
Posts: 492
Joined: Wed Dec 17, 2014 2:55 am

Re: Loop

Postby lmuserx4849 » Sun Dec 03, 2017 4:33 am

Termy wrote:More random information:

  • : is shorthand for true.
    ...


Type the following: help : or type -a :
Null command.

No effect; the command does nothing.

Exit Status:
Always succeeds.

: is a shell builtin


It isn't a shorthand for true. The exit status of the shell builtin "always succeeds". In the bash man page, section "SHELL BUILTIN COMMANDS", it says:
: [arguments]
No effect; the command does nothing beyond expanding arguments and performing any specified redirections. A zero exit code is returned.


You can do things like:

Code: Select all

function myFunc() {
  :
}

: >"${playlist}"


A best practice is to say what you mean, rather than taking advantage of a side effect of a command. It makes the code more readable and decreases the chances for errors in the future. Type the following: type -a true (while true; do).

You might like the unofficial Bash FAQ and the google shell style guide.

User avatar
xenopeek
Level 24
Level 24
Posts: 21465
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Loop

Postby xenopeek » Sun Dec 03, 2017 5:19 am

lmuserx4849 wrote:It isn't a shorthand for true.

Yet this does exactly the same as OP's command:
while :; do echo -n "Mrmajik45 "; done
Image

lmuserx4849
Level 4
Level 4
Posts: 492
Joined: Wed Dec 17, 2014 2:55 am

Re: Loop

Postby lmuserx4849 » Sun Dec 03, 2017 4:13 pm

xenopeek wrote:
lmuserx4849 wrote:It isn't a shorthand for true.

Yet this does exactly the same as OP's command:
while :; do echo -n "Mrmajik45 "; done


Sorry if my reply was not clear.

Termy's first list item was, ": is shorthand for true". That statement is not correct.

The format of the while command is:

Code: Select all

while
  command list
do
  command list
done


Remember that bash is an interpretted language and words are parsed. For example, those just learning might not know that the single bracket ("[") is a shell builtin command itself and the double bracket ("[[") is a reserved word. They may appear to do the same thing, but they do it differently and errors can occur with one but not the other. Those types of things are important to understand, imho. Bottom line, Termy is just looking for an exit status of 0 or success from a command list, that could be any command. But let's call ":" what it is: the null command.
Last edited by lmuserx4849 on Sun Dec 03, 2017 4:30 pm, edited 2 times in total.

User avatar
xenopeek
Level 24
Level 24
Posts: 21465
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Loop

Postby xenopeek » Sun Dec 03, 2017 4:20 pm

Sure, : doesn't equate to true, but in the loop it serves the same purpose. Without, with single or with double brackets.
Image

lmuserx4849
Level 4
Level 4
Posts: 492
Joined: Wed Dec 17, 2014 2:55 am

Re: Loop

Postby lmuserx4849 » Sun Dec 03, 2017 4:25 pm

xenopeek wrote:Sure, : doesn't equate to true, but in the loop it serves the same purpose. Without, with single or with double brackets.


So does while true which is more indicative of what one is trying to accomplish ;-)

When I read Termy's list, to me, it felt like someone enthusiatically learning stuff about bash but there was some missing pieces. I just wanted to fill in the missing piece.

I have seen it called a synonym. As long as folks understand why and that they are not equal.

User avatar
Misko_2083
Level 1
Level 1
Posts: 17
Joined: Mon Jan 12, 2015 10:17 am

Re: Loop

Postby Misko_2083 » Sun Dec 03, 2017 8:47 pm

like "true", ":" has a return status of 0.
true; echo $? returns 0
:; echo $? also returns 0
That is why it can be used in a while loop
while :; do echo -n "Mrmajik45 "; done which does the same as while true; do echo -n "Mrmajik45 "; done

Now, "false" has a return status 1
false; echo $?
Therefore false would be not true "! :" notice the space between
! :;echo $?
until false ; do echo -n "Mrmajik45 ";done does the same as until ! : ; do echo -n "Mrmajik45 ";done

But ":" is a valid name for a function
You can declare it false
:(){ false; } now if you execute this function :; echo $? it will return status 1
It also won't start a loop while :; do echo -n "Mrmajik45 "; done unless you declare the function like this:(){ true; }

User avatar
Termy
Level 3
Level 3
Posts: 170
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Loop

Postby Termy » Sun Dec 10, 2017 7:19 pm

lmuserx4849 wrote: ---


I'm aware of all that, but I think you're being pedantic here. Maybe it would've been better for me to say that it can be used as shorthand for true. : always has an exit status of zero. What xenopeek said, basically.
Here to help.


Return to “Scripts & Bash”