LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   what is the function of this operator "-" in linux (https://www.linuxquestions.org/questions/linux-newbie-8/what-is-the-function-of-this-operator-in-linux-757716/)

khalidpashamce 09-25-2009 09:03 AM

what is the function of this operator "-" in linux
 
Dear Linux Experts,

I'm New to linux, I'm keen to understand and learn Linux OS, Shell scripting , Can you let me what is function of this operator " - " in linux.

example:
$cut -d \| -f 1,4- file_name1 | paste -d \| file_name2 -

$cut -d \| -f 1,4- file_name1 | paste -d \| - file_name2

Pls explain me its role, Kindly add cc to "pasha@yashasvi.co.in"

pixellany 09-25-2009 09:31 AM

Welcome to LQ!!

Quote:

Kindly add cc to "pasha@yashasvi.co.in"
NO!!--Please do not expect people to send you e-mail answers. Questions and answers should be kept on the forum so everyone can benefit.

I STRONGLY RECOMMEND that you remove your e-mail address above---you are inviting a SPAM attack.

In the examples you show, "-" is not really an "operator" it is a flag to tell the utility that the next character(s) are option specifiers. Look at the man page for just about any command and you will see the general pattern.

"-" is of course the subtraction operator, as in "echo $((5-3))"

i92guboj 09-25-2009 09:44 AM

Quote:

Originally Posted by pixellany (Post 3696819)
Welcome to LQ!!

NO!!--Please do not expect people to send you e-mail answers. Questions and answers should be kept on the forum so everyone can benefit.

I STRONGLY RECOMMEND that you remove your e-mail address above---you are inviting a SPAM attack.

I concur, putting your email address in a public forum is inviting spammers to your account.

Quote:

In the examples you show, "-" is not really an "operator" it is a flag to tell the utility that the next character(s) are option specifiers. Look at the man page for just about any command and you will see the general pattern.
Not even that. There's nothing special about '-'. Each command parses the options on its own way. The fact that *most* tools will recognize tokens starting with '-' as command line flags/options is only a convention, and not anything that's enforced by Linux, or that has a special meaning to the shells or Linux itself in general.

Let's take the tool 'tar', for example, you could do this to extract the files inside the archive 'filename.tar.gz':

Code:

tar -xf filename.tar.gz
But tar will also work if you do:

Code:

tar xf filename.tar.gz
To sum up, it's just up to the tool at hand to parse the arguments and decide what do they mean.

As said, in linux, and conventionally, '-' or '--' are used to identify command line flags, but it is just that: a convention, a trend, just like in DOS the trend was '/' instead.

These "prefixes" have no special meaning at all for the OS or the shell under normal circumstances.

pixellany 09-25-2009 11:17 AM

Perhaps we can agree that "it depends"......;)

The man pages are your friend....

The "-" DOES matter in many cases, even if it is not an "operator"
e.g.:
Code:

[mherring@Ath ~]$ cp R stuff ./stuff2
cp: target `./stuff2' is not a directory
[mherring@Ath ~]$ cp -R stuff ./stuff2
[mherring@Ath ~]$  (copied successfully)


pwc101 09-25-2009 11:26 AM

It can also mean standard input or standard output, in some cases.

So definitely: it depends.

i92guboj 09-25-2009 12:09 PM

Well, I guess it's all a semantic question. This is gonna be boring, I warn you :D

As said, it's up to the tool. It has no meaning for Linux or the shell (and that was the question as I understood it). It's the tool which parses the arguments. You could easily write a program that uses '/foo' like DOS did. Some programs don't use the '-' or '--' prefixes. Some use '--' as a separator to delimit where the flags end so you can use '-' in file names, some don't. Some will use '-' to mean stdout or stdin depending on the case, overall when interacting with pipes. Some others will use some other model, like "opt=value", some others will use rc files of some type. Most of them will combine many of these schemes or even all of them. In addition '-' can be part of a file name, even the as the first character of the file name.

Conclusion: '-' is not special at all. It can be user for whatever purpose, just like most other characters.

You could say "well, then the blank space character is not special either because it can also be part of a file name", but then I'd answer with this:

Code:

~/tmp $ touch -- -file
~/tmp $ ls -l -- -file
-rw-r--r--    1 i92guboj i92guboj        0 Sep 25 18:29 -file

Unlike blank spaces, '-' doesn't need escaping or quoting, because it's not special in any regard. It's just a normal character. And "what will it mean" will only depend on the application that receives the argument, not in Linux, nor even in your shell.

In other words, doing "ls -l" is not different of doing "ls filename" in any sense, other than the effect it produces. ls just sees strings, there's nothing that will tell ls that "-l" is a flag and "filename" a file name, other that its internal logic.

The programs parse tokens, not '-' plus 'something_else'. They read tokens from the argument list, and then parse them and act consequently. It's not <minus><l> what ls sees, bash doesn't catch the minus sign and send 'l' as a special argument (flag) to ls. It's the string '-l' as a whole which ls receives, it could be any other arbitrary string and when ls catches it, it still doesn't know if it's a file name or an option. There's nothing in the minus symbol that marks it as a flag delimiter/starter.

Of course, "cp R" will not work (the intended way). Because it's the way that that tool is programmed, just like wine will accept c:\\foo, samba will accept \\foo, tar will accept xf or -xf without problems, but no other linux program will accept any of these. Each program is different, and no one should give for granted that a program will always accept -f|--foo, which seems to be the standard trend for posix and gnu respectively.

What I want to emphasize is that the Linux semantics, technically speaking, don't threat '-' as a special case. Another different thing (and here, pixellany, I agree with you) is the significance that it might have *for us*, the semantic we perceive as human beings with a mental model which is shaped only by habits and nothing else, which is what I defined above as the "convention" and the "trend for posix and gnu". In that case we both agree that -f|--foo are used often to name command line flags, but that doesn't mean that what we perceive as obvious is obvious at all for the OS or the shell, because in fact, it isn't.

Sorry but being that meticulous to the point of turning into an "ass", but I don't want the original poster to be confused and then shocked when he sees something like this:

Code:

tar xvjpf file.tar.bz2
mplayer vo=x11 foo.avi
wget -q -O -
ls -l -- -my-file-name.doc

We should give nothing for granted and check always the man pages or whatever docs are available :)

SharpyWarpy 09-25-2009 12:09 PM

I think pwc101 is on to what the OP meant. In the case OP mentioned it means "read from standin". Like in this example:
flac -cd sound-file.flac | lame -b 320 - sound-file.mp3
the output of the first command is piped to the second command and the result is an mp3 file.

malekmustaq 09-25-2009 12:46 PM

Quote:

Can you let me what is function of this operator " - " in linux.
"separator flag"

pixellany 09-25-2009 02:03 PM

Quote:

Originally Posted by malekmustaq (Post 3696990)
"separator flag"

No!!! If you read this thread, you will see that almost none of the usages are anything like a "separator flag".

lutusp 09-26-2009 01:23 AM

Quote:

Originally Posted by khalidpashamce (Post 3696795)
Dear Linux Experts,

I'm New to linux, I'm keen to understand and learn Linux OS, Shell scripting , Can you let me what is function of this operator " - " in linux.

example:
$cut -d \| -f 1,4- file_name1 | paste -d \| file_name2 -

$cut -d \| -f 1,4- file_name1 | paste -d \| - file_name2

Pls explain me its role, Kindly add cc to "pasha(AT)yashasvi.co.in"

That depends. What does "I love you" mean? The symbol "-" has just as many meanings. No, wait, it has more.

Code:

Kindly add cc to "pasha(AT)yashasvi.co.in"
Never ask for an e-mail response. This is a public forum, not a private consulting service. Also, get ready to replace your e-mail address.

arizonagroovejet 09-26-2009 04:02 AM

Another usage:

Code:

case:/tmp/foo/boo/moo mike$ cd /tmp
case:/tmp mike$ cd -
/tmp/foo/boo/moo
case:/tmp/foo/boo/moo mike$


gregorian 09-26-2009 09:42 AM

Quote:

Originally Posted by khalidpashamce (Post 3696795)
Pls explain me its role, Kindly add cc to "pasha@yashasvi.co.in"

There's an option to subscribe to this thread so you can receive email notifications.

Quote:

We should give nothing for granted and check always the man pages or whatever docs are available
Call me a fool for attempting man - but what do I look for?

pixellany 09-26-2009 09:53 AM

Quote:

Originally Posted by gregorian (Post 3697749)
There's an option to subscribe to this thread so you can receive email notifications.


Call me a fool for attempting man - but what do I look for?

"-" is not a command, so I would not expect to find a man page for it...

In this context, I think we mean to look at the man page for common commands and see how "-" is used.

i92guboj 09-27-2009 12:38 AM

Quote:

Originally Posted by gregorian (Post 3697749)
There's an option to subscribe to this thread so you can receive email notifications.


Call me a fool for attempting man - but what do I look for?

Mmmmmm, if you want to take it personal it's your problem, but... during all that post, I quoted "pixellany", not you, sorry, and you are taking this out of context, completely, I only wanted to help. Sorry. I quit.

SharpyWarpy 09-27-2009 08:36 AM

To the OP: "man bash" would be the best place for you to start if you want a comprehensive guide. Also look here for a very good advanced bash guide:
http://tldp.org/LDP/abs/html
You can even download it in PDF format. I love it.


All times are GMT -5. The time now is 04:35 PM.