Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • what does printf '%s\n' do ? Commented Feb 22, 2020 at 20:35
  • 2
    @ForzaTekk printf prints all the arguments from its 2nd argument onward according the the formatting in its first argument. The format string %s\n specifies that each string should be printed as a string (duh) with a newline after it. The ed editor reads editing commands from standard input and expects these to be on separate lines, that's why. Run the printf command by itself (up to, but not including the |) to see what gets passed to ed. Also try printf 'name: %s\n' * (without the ed bit, as an educational exercise). Commented Feb 22, 2020 at 20:43
  • Thank you, this is magical. A variant using echo instead of printf echo '11m0\nwq' | ed -s bonjour. Also (in case it saves someone time), ed handles range of lines too: echo 11,13m0\nwq | ed -s bonjour. Commented Jul 20, 2024 at 5:07
  • "Also try printf 'name: %s\n' * (without the ed bit, as an educational exercise)" – Another TIL! Commented Jul 20, 2024 at 5:19
  • @Manav Remember that echo may not output \n as a literal newline (or support any other backslash sequences) on some systems or in some shells (it is not standard behaviour). In shells that support it, it could also be turned off by default, or by a user setting (e.g. shopt -u xpg_echo in bash). Using printf is therefore more portable. Commented Jul 20, 2024 at 9:31