0

I sometimes need a command like pass in Python in my bash scripts.

Like:

if grep something
then
    pass
else
    code
fi

In Python you have:

>>> for element in a:
...     if not element:
...         pass
...     print element

QUESTION:

I always use continue but it gives an error that it should only be used in a for, while or until loop.

What would you do in this circumstance?

  • What does pass do in python? – roaima Oct 18 '18 at 6:33
  • 1
    Saying that this is a duplicate doesn’t imply that your question isn’t useful, on the contrary: the duplicate system allows multiple questions, which will be kept if they are useful, to point to the same answer. – Stephen Kitt Oct 18 '18 at 6:37
  • @StephenKitt yea but if it is there wont come any new answers and that could give more or something that was not in the other question – somethingSomething Oct 18 '18 at 6:39
  • 2
    More seriously, perhaps you could revisit your question title. The title as it stands is fully answered by the duplicate, but it’s not entirely representative of the contents of the question — because in the circumstances you describe, I would flip the condition rather than using a do-nothing command (both in shell and in Python). – Stephen Kitt Oct 18 '18 at 7:44
  • 1
    Your Python example is somewhat silly, since the "if" isn't doing anything there, it's going to print elements even if they evaluate to false... Perhaps you were thinking of continue there? To use something with pass you would need to have an else: block there (but that would make the check for not element silly, since then it becomes a double negative and you don't get the benefits of shallower indentation you'd get with a continue). – filbranden Oct 18 '18 at 15:25
9

Your title is fully answered by A do nothing line in a bash script: : or true are effectively equivalent to pass.

However in these circumstances I would flip the condition:

if ! grep something
then
    code
fi

and

>>> for element in a:
...     if element:
...         print element
  • Also, grep -q, depending on whether the script is supposed to output the matches or not. – Kusalananda Oct 18 '18 at 7:46
  • Better IMO is grep something || { code; }, using || for when a command fails rather than an if. – filbranden Oct 18 '18 at 15:28
1

That's a ugly kludge, but if you really want to do that you could use

usleep

This command pauses the execution for 1 microsecond.

You could use it in a function:

function do_nothing {
   usleep
}

and then use the function like Python's pass.

It works but is not very nice to read and would break the logic flow of the script, so I don't recommend to use it. A much better solution is to rewrite the script instead as @StephenKitt suggested.

  • This is an even uglier kludge IMHO – xenoid Oct 18 '18 at 8:57
  • @xenoid Uglier than what? – dr01 Oct 18 '18 at 9:54
  • @dr01 Thank you for your input, it was helpful – somethingSomething Oct 18 '18 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.