Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does minus mean in "exec 3>&-" and how do I use it?

I often have trouble figuring out certain language constructs because they won't register when googling or duckduckgoing them. With a bit of experimenting, it's often simple to figure it out, but I don't get this one.

I often see stuff like 2>&1 or 3>&- in bash scripts. I know this is some kind of redirection. 1 is stdout and 2 is stderror. 3 is probably custom. But what is the minus?

Also, I have a script whose output I want to log, but also want to see on screen. I use exec > >(tee $LOGFILE); exec 2>&1 for that. It works. But sometimes when I bashtrap this script, I cannot type at the prompt anymore. Output is hidden after Ctrl+C. Can I use a custom channel and the minus sign to fix this, or is it unrelated?

like image 513
Redsandro Avatar asked Dec 28 '12 08:12

Redsandro


People also ask

What does minus mean in Linux?

but it usually means the stdout/stdin in bash commands. In this case, - is the argument to the -O option, so the downloaded data is not saved in a file, but printed to stdout, so it can be piped to the tar command.

What does $? Mean in bash?

$? - It gives the value stored in the variable "?". Some similar special parameters in BASH are 1,2,*,# ( Normally seen in echo command as $1 ,$2 , $* , $# , etc., ) . Follow this answer to receive notifications. edited Jun 20, 2020 at 9:12.

What does hyphen mean in Linux?

- (Hyphen.) Expands to the current option flags (the single-letter option names concatenated into a string) as specified on invocation, by the set builtin command, or implicitly by the shell. $ Expands to the process ID of the invoked shell.

What is exec in shell script?

The exec command is a powerful tool for manipulating file-descriptors (FD), creating output and error logging within scripts with a minimal change. In Linux, by default, file descriptor 0 is stdin (the standard input), 1 is stdout (the standard output), and 2 is stderr (the standard error).


1 Answers

  1. 2>&1 means that stderr is redirected to stdout
  2. 3>&- means that file descriptor 3, opened for writing(same as stdout), is closed.

You can see more examples of redirection here

  1. As for questions number 3, I think this is a good link.
like image 138
banuj Avatar answered Sep 28 '22 04:09

banuj