Does “ - ” mean stdout in bash?

后端 未结 4 1672
迷失自我
迷失自我 2020-12-10 04:25

Is \" - \" a shortcut for stdout in bash? If not what does it mean? For example,

wget -q -O - $line 

How about stdin?

Thanks and re

相关标签:
4条回答
  • 2020-12-10 05:00

    It depends on the program. Ususally, - means "send to the output".

    Use read:

    #!/bin/bash
    
    echo -n "What's your name? "
    read var1
    echo "hello $var1"
    
    0 讨论(0)
  • 2020-12-10 05:01

    - is not special in bash, it is simply given as a parameter to the program. Then it depends on the program how it interprets that -.

    Commonly it denotes that stdin or stdout should be used, depending on context.

    0 讨论(0)
  • 2020-12-10 05:04

    - is just a convention used by wget (and quite a few other tools) to indicate the output is to be sent to stdout. It's not part of bash, but it's a special case treated by the command itself (some commands will end up creating a "-" file if you assume this is the case). You could replace it with /dev/stdout (and you can generally use /dev/stdin as an input file when appropriate).

    0 讨论(0)
  • 2020-12-10 05:10

    As far as I know, bash isn't involved with the usage of dash. It's just a convention of many UNIX command line utilities to accept - as a placeholder for stdin or stdout when put in place of an input or output file name on the command line.


    Edit: found it, this behavior is specified in the POSIX Utility Syntax Guidelines, §12.2.13 of The Open Group Base Specifications:

    For utilities that use operands to represent files to be opened for either reading or writing, the '-' operand should be used only to mean standard input (or standard output when it is clear from context that an output file is being specified).
    0 讨论(0)
提交回复
热议问题