Page 1 of 1

Loop

Posted: Sat Nov 11, 2017 1:49 pm
by mrmajik45

Code: Select all

while true; do echo -n "Mrmajik45 "; done
Replace mrmajik45 with whatever you want it to say.

Re: Loop

Posted: Sat Nov 11, 2017 1:52 pm
by xenopeek
Let's optimize that! This command does the exact same thing :wink:
yes Mrmajik45

Ctrl+C to interrupt the command.

Re: Loop

Posted: Sat Nov 11, 2017 2:31 pm
by mrmajik45
That could be the Straight line version.

Re: Loop

Posted: Sat Nov 11, 2017 3:00 pm
by xenopeek
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 :)

Re: Loop

Posted: Sun Nov 26, 2017 1:18 pm
by Termy
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.

Re: Loop

Posted: Sun Dec 03, 2017 4:33 am
by lmuserx4849
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.

Re: Loop

Posted: Sun Dec 03, 2017 5:19 am
by xenopeek
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

Re: Loop

Posted: Sun Dec 03, 2017 4:13 pm
by lmuserx4849
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.

Re: Loop

Posted: Sun Dec 03, 2017 4:20 pm
by xenopeek
Sure, : doesn't equate to true, but in the loop it serves the same purpose. Without, with single or with double brackets.

Re: Loop

Posted: Sun Dec 03, 2017 4:25 pm
by lmuserx4849
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.

Re: Loop

Posted: Sun Dec 03, 2017 8:47 pm
by Misko_2083
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; }

Re: Loop

Posted: Sun Dec 10, 2017 7:19 pm
by Termy
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.