What does >& mean in command? [duplicate] - linux

This question already has answers here:
What does " 2>&1 " mean?
(19 answers)
Closed 8 years ago.
module load PrgEnv/intel/default >& /dev/null
What is the meaning of >& in this command?

Let's look it up in man bash:
Redirecting Standard Output and Standard Error
This construct allows both the standard output (file descriptor 1) and the stan‐
dard error output (file descriptor 2) to be redirected to the file whose name is
the expansion of word.
There are two formats for redirecting standard output and standard error:
&>word
and
>&word
Of the two forms, the first is preferred. This is semantically equivalent to
>word 2>&1
In other words, >& /dev/null suppresses both output and error/informational messages from the command.

Related

Assigning custom file descriptors in bash and writing to it [duplicate]

This question already has answers here:
What does the ampersand indicate in this bash command 1>&2
(7 answers)
How do file descriptors work?
(4 answers)
Closed 28 days ago.
I was playing with bash a bit and was trying to assign a custom descriptor to a file to read from or write to.
Here is what i did:
exec 5> hello.txt # assigned 5 to hello.txt
echo "hello brother" >& 5 # works
Now the thing that i am not understanding is that if i do this instead
echo "some text" > 5 # does not work, creates a file named '5'
It does not work like the above >&.
I understand:
>& redirects stdout & stderr
> redirects stdout
What is it that i am missing, is >& necessary when using descriptor?
I understand:
This is exactly the problem - >&5 is ambiguous. That's why you shouldn't use >& and prefer > file 2>&1. See https://wiki.bash-hackers.org/scripting/obsolete
There is a special logic, that if the next token after & is a digit, then it's redirection of stdout to a file descriptor > &5. https://github.com/bminor/bash/blob/ec8113b9861375e4e17b3307372569d429dec814/parse.y#L645

Passing multiple arguments in Bash Script containing space [duplicate]

This question already has answers here:
Iterate over a list of files with spaces
(12 answers)
How to iterate over arguments in a Bash script
(9 answers)
Closed 5 years ago.
I just started learning Bash Script.
My script is accepting 1 to n number of arguments. Each argument is pass to a function rename.
My problem is that the argument I pass does not accept space.
script.sh
#!/bin/bash
for FILE in $#
do
echo "$FILE"
rename $FILE
done
For Ex:
./script.sh /Users/xyz/Section 5/abc/ /Users/xyz/pqr/ /Users/z/abc
The above argument "/Users/xyz/Section 5/abc/" should be one even if it contains space.
But the code in script.sh will break it into two argument.
So the output is:
/Users/xyz/Section
5/abc/
/Users/xyz/pqr/
/Users/z/abc
But My Expected Output should be:
/Users/xyz/Section 5/abc/
/Users/xyz/pqr/
/Users/z/abc
Note: Different solution I tried till now:
1) "/Users/xyz/Section 5/abc/" --> Same output ie 2 different argument
2) '/Users/xyz/Section 5/abc/' --> Same output ie 2 different argument
3) /Users/xyz/Section\ 5/abc/ --> Same output ie 2 different argument

How to read hyperlink into variable [duplicate]

This question already has an answer here:
Reading input in bash in an infinite loop and reacting to it
(1 answer)
Closed 6 years ago.
I need to input into variable hyperlink and then download it via wget
Link is:
http://st-im.xyz.com/im/poster/2/1/0/xyz.com-Qwerty-2107544.jpg
But it seems like bash is not writing my link into variable. So, how can I fix this?
Code:
read $link
echo "Link is"
echo $link
wget "$link"
Output:
http://st-im.xyz.com/im/poster/2/1/0/xyz.com-Qwerty-2107544.jpg # This is my input
Link is
http://: Invalid host name.
You are reading the value wrong.
It should be:
read link
$variable is for variable substitution. (Retrieving the value). Not for assigning the value.

What does >& mean here?

I have C code, that has been compiled. Then I have to execute from command line
../../../PStomo_eq665/pstomo_eq par=syn.par >& log.syn
What does >& mean in this context? Both files syn.par and log.syn contain parameters for pstomo_eq.
Redirect stderr and stdout
>& is equivalent to &> and redirects both standard error and standard output.
From the bash man page:
There are two formats for redirecting standard output and standard
error:
&>word
and
>&word
Of the two forms, the first is preferred. This is semantically
equivalent to:
>word 2>&1
In your question, stderr and stdout are redirected to log.syn

Difference between "command > log.txt" and "command 1>& log.txt" in Linux command shell?

When I run the command haizea -c simulated.conf > result.txt, the program (haizea) still prints its output to the screen. But when I try haizea -c simulated.conf 1>& result.txt, the output is now on the file result.txt. I'm quite confused about this situation. What is the difference between > and 1>&, then?
What you're seeing on the terminal is the standard error of your process. Both of these are directed to the same terminal device by default (assuming no redirection put into effect).
The redirection >&xyz redirects both standard output and error to the file xyz.
I've never used it but I would think, by extension, that N>&xyz would redirect file handle N and standard error to your file. So 1>&xyz is equivalent to >&xyz which is also equivalent to >xyz 2>&1.
The number before the > stands for the descriptor.
Standard Input - 0
Standard Output - 1
Standard Error - 2
The & will direct both standard output and standard error.
http://linuxdevcenter.com/pub/a/linux/lpt/13_01.html#doc2ac15b1c13
> redirects standard output alone.
>& or &> or 1>& redirect both standard output and standard error.
Your program is printing on standard error which is not getting redirected in case 1.

Resources