Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does '-' stand for in bash?

Tags:

linux

bash

unix

What exactly are the uses of '-' in bash? I know they can be used for

  1. cd - # to take you to the old 'present working directory'
  2. some stream generating command | vim - # somehow vim gets the text.

My question is what exactly is - in bash? In what other contexts can I use it?

Regards Arun

like image 407
trybeingarun Avatar asked May 08 '10 16:05

trybeingarun


People also ask

What is $- in bash?

$- (dollar hyphen) bash parameter is used to get current option flags specified during the invocation, by the set built-in command or set by the bash shell itself.

What is $() called in bash?

It turns out, $() is called a command substitution. The command in between $() or backticks (“) is run and the output replaces $() . It can also be described as executing a command inside of another command.

What is '- E used for in Linux?

Set –e is used within the Bash to stop execution instantly as a query exits while having a non-zero status. This function is also used when you need to know the error location in the running code.

What does [[ ]] mean in bash?

The [[ ... ]] part allows to test a condition using operators. Think of it as an if statement. In your example, you're using the -s operator, which tests that the referenced file is not empty. Copy link CC BY-SA 3.0.


2 Answers

That depends on the application.

cd -

returns to the last directory you were in.

Often - stands for stdin or stdout. For example:

xmllint -

does not check an XML file but checks the XML on stdin. Sample:

xmllint - <<EOF
<root/>
EOF

The same is true for cat:

cat -

reads from stdin. A last sample where - stands for stdout:

wget -O- http://google.com

will receive google.com by HTTP and send it on stdout.

By the way: That has nothing to do with your shell (e.g. bash). It's only semantics of the called application.

like image 162
Johannes Weiss Avatar answered Sep 29 '22 22:09

Johannes Weiss


- in bash has no meaning as a standalone argument (I would not go as far as to say it it does not have a meaning in shell at all - it's for example used in expansion, e.g. ls [0-9]* lists all files starting with a digit).

As far as being a standalone parameter value, bash will do absolutely nothing special with it and pass to a command as-is.

What the command does with it is up to each individual program - can be pretty much anything.

There's a commonly used convention that - argument indicates to a program that the input needs to be read from STDIN instead of a file. Again, this is merely how many programs are coded and technically has nothing to do with bash.

like image 33
DVK Avatar answered Sep 29 '22 21:09

DVK