What's the Magic of "-" (A Dash) in Command-Line Parameters

What's the magic of - (a dash) in command-line parameters?

If you mean the naked - at the end of the tar command, that's common on many commands that want to use a file.

It allows you to specify standard input or output rather than an actual file name.

That's the case for your first and third example. For example, the cdrecord command is taking standard input (the ISO image stream produced by mkisofs) and writing it directly to /dev/dvdrw.

With the cd command, every time you change directory, it stores the directory you came from. If you do cd with the special - "directory name", it uses that remembered directory instead of a real one. You can easily switch between two directories quite quickly by using that.

Other commands may treat - as a different special value.

In this bash script, what does - mean? And how could one find out what it means?

This command line is just badly formatted. You're reading into something that isn't there. Some Blog software author thought it was a smart idea to auto-reformat the article so that hyphens and such were long dashes and quotes were "smart", etc. In the end, somehow a space ended up after the dash, before the next parameter.

For example, let's look at this:

node /app/ganache-core.docker.cli.js — quiet

Even if we assume that's a regular hyphen -, we know it's not supposed to have a space after it. It's supposed to be -quiet. And, if you have any doubt about this, you can read in the source code where this is defined:

.option('q', {
group: 'Other:',
alias: 'quiet',
describe: 'Run ganache quietly (no logs)',
type: 'boolean',
default: false
})

The same is true for -account.

And I took away that it CAN mean standard input... if the authors of the bash program define it as such.

Yes, that's correct. I don't know what this software does, but if it's reading from STDIN, it's not because you told it to on the command line. It's because that's what it does.

Passing Command Line Arguments that contain hyphens

It seems that 12d.exe does not understand some valid filenames, shame on 12d.exe.

There are a couple actions you might do,

  1. try to pass the short filename instead. It may work in some cases where the hyphen - is off the right of the string. Not in your case, though, as it will probably translate as STAFF-~1 or something similar.

    call :invoke12d "Staff - Name\abc.4dm"
    goto :eof
    :invoke12d
    12d %~s1
    goto :eof
  2. or create a junction, a symbolic link, to call the directory with another name that does not contain hyphens. See this http://technet.microsoft.com/en-us/sysinternals/bb896768 for more information and to download a simple tool.

    junction staffnam "Staff - Name"
    12d.exe "staffnam/abc.4dm"

What does the -; mean at the end of a bash statement?

The semicolon ends the command (to end the pipeline is, I believe, technically the right way to say it). You could follow it with another command if you wanted to, as in

bwa sampe ref.fa r1.sai r2.sai r1.fq r2.fq | samtools view -bSho out.bam -; echo Here is another command.

Otherwise, the semicolon is harmless but probably unnecessary.

Regarding the - hyphen that precedes the semicolon, for samtools and many other commands it means to use standard input in place of an input file (or, in some cases, standard output in place of an output file). This is typical Linux/Unix usage.

(Thanks to @phatfingers for verifying the usage of the samtools command.)

Command line parameters, standard aproach to parse?

On systems like Linux, there is the convention that options that are full words use two dashes (e.g. --file), while single-letter options use a single dash (e.g. -f.)

Using slash to introduce options is from old DOS, and is being kept in Windows.

Also, if an option is using a whole word, it can not be split into several options. This is in regards to your example with -file: -file can either be one option, or four different options (-f, -i, -l and -e).

All in all, how options looks, or is handled, differ very much between programs, and there really isn't any specific standard.

I would suggest you find some way you like, and then use that.

What does this python - do?

This has nothing to do with bash. - has a very specific meaning when passed to the python binary:

-

Read commands from standard input (sys.stdin). [...]

Since, in your example,

  • curl outputs the downloaded file to stdout and
  • the shell pipe | passes curl's output to python's stdin,

python will execute the commands contained in the file downloaded by curl.


Note that this is a convention commonly found in various command-line utilities: Providing a single hyphen in place of a file name causes the command to read input from stdin instead of a file.

How to create shell variable with dashes?

I've never met a Bourne-style shell that allowed - in a variable name. Only ASCII letters (of either case), _ and digits are supported, and the first character must not be a digit.

If you have a program that requires an environment variable that doesn't match the shell restrictions, launch it with the env program.

env 'strange-name=some value' myprogram

Note that some shells (e.g. modern dash, mksh, zsh) remove variables whose name they don't like from the environment. (Shellshock has caused people to be more cautious about environment variable names, so restrictions are likely to become tighter over time, not more permissive.) So if you need to pass a variable whose name contains special character to a program, pass it directly, without a shell in between (env 'strange-name=some value' sh -c'…; myprogram' may or may not work).

https://unix.stackexchange.com/questions/23659/can-shell-variable-name-include-a-hyphen-or-dash

What is the purpose of the - in sh script line: ext=$(echo $ext | sed 's/\./\\./' -)

On my system, that syntax isn't documented in the man page, but it is in the
'info' page:

sed OPTIONS... [SCRIPT] [INPUTFILE...]

If you do not specify INPUTFILE, or if INPUTFILE is -',sed'
filters the contents of the standard input.

Given that particular usage, I think you could leave off the '-' and it should
still work.

Read from a file and stdin in Bash

You can use cat - or cat /dev/stdin:

while read line; do
# your code
done < <(cat "$1" -)

or

while read line; do
# your code
done < <(cat "$1" /dev/stdin)

or, if you want to read from all files passed through command line as well as stdin, you could do this:

while read line; do
# your code
done < <(cat "$@" /dev/stdin)

See also:

  • How to read from a file or stdin in Bash?


Related Topics



Leave a reply



Submit