Results 1 to 9 of 9

Thread: '-n 3' vs '-n3' - is the space necessary?

  1. #1
    Join Date
    Oct 2013
    Beans
    97
    Distro
    Ubuntu 12.04 Precise Pangolin

    '-n 3' vs '-n3' - is the space necessary?

    I have accidentally discovered that
    Code:
    tail -n3 story.txt
    works just as well as
    Code:
    tail -n 3 story.txt
    I used the command "tail" as an example, but I've tried it with a few other commands also, and there it worked fine also.

    So my question is: Is the space necessary? Or maybe formulated a bit more precisely: Are there commands that will not accept the version without the space?
    Nunca te acostarás, sin saber una cosa más.

  2. #2
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,297
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: '-n 3' vs '-n3' - is the space necessary?

    It obviously depends on how the program processes its parameters. If it uses getopt(), then the space is optional. Most programs you will encounter use it, but of course some may not...

    P.S.: Note also that, in principle, you should always put a space. Omitting it is only permitted so as to not break old applications.
    Last edited by Bachstelze; October 24th, 2013 at 01:33 AM.
    「明後日の夕方には帰ってるからね。」


  3. #3
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: '-n 3' vs '-n3' - is the space necessary?

    Quote Originally Posted by Bachstelze View Post
    P.S.: Note also that, in principle, you should always put a space. Omitting it is only permitted so as to not break old applications.
    This. I've always included spaces in options. It's a good thing to get in the habit of.
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,393
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: '-n 3' vs '-n3' - is the space necessary?

    i don't think ffmpeg would work with compressed parameters.
    http://linux.die.net/man/1/ffmpeg
    too many of them would be ambiguous if space was optional. It can only work when you have unique 1 char switches that can be optionally glued together and the value tacked at the end is assumed to be assigned to the last one, but ffmpeg uses shortparam convention of single - for words and that would mean many collisions (eg -s -ss -scodec)
    Last edited by Vaphell; October 24th, 2013 at 12:12 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  5. #5
    Join Date
    Oct 2013
    Beans
    97
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: '-n 3' vs '-n3' - is the space necessary?

    Thanks guys, I understand. So in short, always use the space
    Nunca te acostarás, sin saber una cosa más.

  6. #6

    Re: '-n 3' vs '-n3' - is the space necessary?

    There are a few cases where an option takes an argument, but it won't work if you use a space. I think sed's -i and Perl's -F (but not awk's) fall into this category.

    When in doubt, read the manual.

  7. #7
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,297
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: '-n 3' vs '-n3' - is the space necessary?

    Quote Originally Posted by trent.josephsen View Post
    There are a few cases where an option takes an argument, but it won't work if you use a space. I think sed's -i and Perl's -F (but not awk's) fall into this category.

    When in doubt, read the manual.
    Yes, when the arguent is optional (as in sed -i), then the space is mandatory, otherwise the program can't distinguish between an option and its argument or several options put together (sed -ibak means sed -i -b -a -k, not sed -i bak).

    (Also sed -i is non-standard. For standard-compliant programs, optional arguments are discouraged.)
    「明後日の夕方には帰ってるからね。」


  8. #8
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,393
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: '-n 3' vs '-n3' - is the space necessary?

    (sed -ibak means sed -i -b -a -k, not sed -i bak).
    no, it's in fact -i bak. When switches are glued together -i has to be last because everything that goes after will be used as a filename suffix

    Code:
    $ touch sed-i.txt
    $ ls sed-*
    sed-i.txt
    $ sed -inrbak 's/$//' sed-i.txt
    $ ls sed-*
    sed-i.txt  sed-i.txtnrbak
    $ sed -rnibak 's/$//' sed-i.txt
    $ ls sed-*
    sed-i.txt  sed-i.txtbak  sed-i.txtnrbak
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  9. #9
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,297
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: '-n 3' vs '-n3' - is the space necessary?

    Oh, okay, as you can see I hadn't tested it...

    (And sed -i is non-standard, rawr.)
    「明後日の夕方には帰ってるからね。」


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •