I saw a command to fix issues while running cron jobs on stackoverflow. https://stackoverflow.com/questions/21926465/issues-running-cron-in-docker-on-different-hosts I am ntsure what exactly this command does since I am new to unix commands. Can any noble soul spell out what each part does ?

sed --regexp-extended --in-place 's/^session\s+required\s+pam_loginuid.so$/session optional pam_loginuid.so/' /etc/pam.d/crond

  • 2
    Before we start, do you know anything at all about sed? Have you read man sed?
    – roaima
    Apr 26 at 21:01
  • 2
    What's the problem you're actually trying to solve? This is an incomplete command line (it ends with && which is a logical and with a continuation to another command)
    – roaima
    Apr 26 at 21:03
  • Would be good also if you share the link to the original answer on stackoverflow, so we can get some background information.
    – pLumo
    Apr 27 at 9:23
  • Added the details of the original answer i found on stackoverflow. It would be nice if you folks removed the downvotes you did ? Apr 27 at 9:55

1 Answer 1


sed is a command-line text editor, used like sed -flags command file, which is most commonly used for doing find-and-replace with regular expressions. It can do a lot, I find https://www.grymoire.com/Unix/Sed.html to be a good beginner-ish guide (friendlier than the man page)

Regular expressions (regex) are a bit like a complicated system of wildcards, that lets you look for patterns of text rather than specific things

--regexp-extended tells it to use extended regular expressions, which have more features

--in-place tells it to change the file, rather than printing the output to the shell or stdout

's/foo/bar' is the substitute command for sed (s stands for substitute), which finds the first 'foo' on every line and swaps it for 'bar'

^session: the ^ means the start of a line in regex

\s+: \s means "any whitespace", so space or tab or similar, in regex; + means one or more, so this matches one or more spaces/tabs/etc. + needs extended regex

...nuid.so$: the $ means the end of a line in regex

So, this is finding every line that's like session optional pam_loginuid.so but with more spaces/tabs, and replacing it with session optional pam_loginuid.so with one space, and with required replaced with optional, in the file /etc/pam.d/crond

The && means "run the next command if this one returns zero", and most things in bash return zero if they don't crash. sed returns zero even if it doesn't find anything to substitute, so this will almost always happen

The \ at the end makes it ignore the newline, so the command on the next line gets executed too, unless something doesn't work

So basically it's changing required to optional and removing extra spaces, but hopefully this helps you figure out what sed 's/thing/otherThing/' does in general


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.