What does the "-" operator actually do on Linux?

I see that the operator - behaves differently with different teams.

For instance,

 cd - 

cds to the previous directory, whereas

 vim - 

read from stdin

So, I want to know why the operator - behaves in two different ways. Can someone point me to the detailed documentation of the operator - ?

+4
source share
4 answers

This is not an operator, this is an argument. When you write a program in C or C ++, it comes as argv[1] (when this is the first argument), and you can do whatever you like with it.

By convention, many programs use - as a placeholder for stdin, where the input file name is usually required, and stdout, where the output file name is expected. But cd does not require reading a file stream, why does it need stdin or stdout?

Optional: the following is a vim main.c snippet that parses arguments starting with - : if there is no extra character, it activates the STDIN input.

  else if (argv[0][0] == '-' && !had_minmin) { want_argument = FALSE; c = argv[0][argv_idx++]; #ifdef VMS ... #endif switch (c) { case NUL: /* "vim -" read from stdin */ /* "ex -" silent mode */ if (exmode_active) silent_mode = TRUE; else { if (parmp->edit_type != EDIT_NONE) mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]); parmp->edit_type = EDIT_STDIN; read_cmd_fd = 2; /* read from stderr instead of stdin */ } 
+11
source

Silence in itself is a simple argument to the team. Its meaning depends on the team. Its two most common meanings are "standard input" or (less commonly) "standard output". The value of the “previous directory” is unique to the built-in cd shell shell (and this only means that in some shells, and not in all shells).

 cat file1 - file2 | troff ... 

This means that in this sequence read file1 , standard input and file2 and send the result to troff .

An extreme use case - to mean "standard input" or "standard output" comes from (GNU) tar :

 generate_file_list ... | tar -cf - -T - | ( cd /some/where/else; tar -xf - ) 

Parameters -cf - in the first tar means "create archive", and "output file means standard output"; option -T - means "read the list of files and / or directories from standard input".

The -xf - options -xf - in the second tar means "extract archive" and "input file - standard input". In fact, GNU tar has the -C /some/where/else option, which means it runs cd itself, so the whole command could be:

 generate_file_list ... | tar -cf - -T - | tar -xf - -C /some/where/else 

The net effect of this is to copy the files called by the generate_file_list command from the "current directory" to /some/where/else , preserving the directory structure. (The "current directory" should be taken with a small pinch of salt, any absolute file names receive special GNU tar processing - it removes the leading slash and mdash, and relative names are taken relative to the current directory.)

+2
source

It depends on the program used. This means different things for different programs.

+1
source

I think different programs use different conventions. manpages shows how each program is interpreted - here man bash

 - At shell startup, set to the absolute pathname used to invoke the shell or shell script being executed as passed in the environment or argument list. Subsequently, expands to the last argument to the previous command, after expansion. Also set to the full pathname used to invoke each command executed and placed in the environment exported to that command. When checking mail, this parameter holds the name of the mail file currently being checked. 

and man vim

  - The file to edit is read from stdin. Commands are read from stderr, which should be a tty. 
0
source

Source: https://habr.com/ru/post/1400960/


All Articles