Loop

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
mrmajik45

Loop

Post by mrmajik45 »

Code: Select all

while true; do echo -n "Mrmajik45 "; done
Replace mrmajik45 with whatever you want it to say.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29507
Joined: Wed Jul 06, 2011 3:58 am

Re: Loop

Post by xenopeek »

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

Ctrl+C to interrupt the command.
Image
mrmajik45

Re: Loop

Post by mrmajik45 »

That could be the Straight line version.
User avatar
xenopeek
Level 25
Level 25
Posts: 29507
Joined: Wed Jul 06, 2011 3:58 am

Re: Loop

Post 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 :)
Image
User avatar
Termy
Level 12
Level 12
Posts: 4254
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Loop

Post 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.
I'm also Terminalforlife on GitHub.
lmuserx4849

Re: Loop

Post 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.
User avatar
xenopeek
Level 25
Level 25
Posts: 29507
Joined: Wed Jul 06, 2011 3:58 am

Re: Loop

Post 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
Image
lmuserx4849

Re: Loop

Post 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.
Last edited by lmuserx4849 on Sun Dec 03, 2017 4:30 pm, edited 2 times in total.
User avatar
xenopeek
Level 25
Level 25
Posts: 29507
Joined: Wed Jul 06, 2011 3:58 am

Re: Loop

Post by xenopeek »

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

Re: Loop

Post 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.
Misko_2083

Re: Loop

Post 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; }
User avatar
Termy
Level 12
Level 12
Posts: 4254
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Loop

Post 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.
I'm also Terminalforlife on GitHub.
Locked

Return to “Scripts & Bash”