stderr redirection by reading a file [duplicate]
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
This question already has an answer here:
How to make bash substitution $(<âfilenameâ) silent
4 answers
I read content of the file into variable like this
var=$(<somefile)
But if the file doesn't exist I get accordingly an error message
bash: somefile: No such file or directory
Is it possible to redirect stderr to >/dev/null without using cat command?
bash io-redirection stderr
marked as duplicate by Jeff Schaller, Community⦠Mar 14 at 10:51
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
2
down vote
favorite
This question already has an answer here:
How to make bash substitution $(<âfilenameâ) silent
4 answers
I read content of the file into variable like this
var=$(<somefile)
But if the file doesn't exist I get accordingly an error message
bash: somefile: No such file or directory
Is it possible to redirect stderr to >/dev/null without using cat command?
bash io-redirection stderr
marked as duplicate by Jeff Schaller, Community⦠Mar 14 at 10:51
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
This question already has an answer here:
How to make bash substitution $(<âfilenameâ) silent
4 answers
I read content of the file into variable like this
var=$(<somefile)
But if the file doesn't exist I get accordingly an error message
bash: somefile: No such file or directory
Is it possible to redirect stderr to >/dev/null without using cat command?
bash io-redirection stderr
This question already has an answer here:
How to make bash substitution $(<âfilenameâ) silent
4 answers
I read content of the file into variable like this
var=$(<somefile)
But if the file doesn't exist I get accordingly an error message
bash: somefile: No such file or directory
Is it possible to redirect stderr to >/dev/null without using cat command?
This question already has an answer here:
How to make bash substitution $(<âfilenameâ) silent
4 answers
bash io-redirection stderr
asked Mar 14 at 8:37
renton
111
111
marked as duplicate by Jeff Schaller, Community⦠Mar 14 at 10:51
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Jeff Schaller, Community⦠Mar 14 at 10:51
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
4
down vote
In this case, either use cat
:
var=$( cat somefile 2>/dev/null )
or test for file existence first:
if [ -f somefile ]; then
var=$(<somefile)
fi
To avoid the race condition in the last example (the file might theoretically disappear between the test and reading it):
if cp somefile myname 2>/dev/null; then
var=$(<myname)
rm -f myname
fi
The following will not work:
var=$(<somefile) 2>/dev/null
var=$(<somefile 2>/dev/null)
This is the pettiest gripe, but the second fix has a race condition...
â Michael Homer
Mar 14 at 9:04
@MichaelHomer It has. The file might theoretically disappear between the test and reading it. Another way would be to copy the file and read it if that succeeded, but thecat
is probably best (and also portable).
â Kusalananda
Mar 14 at 9:25
add a comment |Â
up vote
2
down vote
You can use:
var=$(<somefile); 2> /dev/null
Hmm... yes, that would be best. Didn't we have this in an answer the other day (I can't find it)?
â Kusalananda
Mar 14 at 10:28
@Kusalananda, yes, I couldn't find it either, but I'm pretty sure there's a recent dup too.
â Stéphane Chazelas
Mar 14 at 10:35
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
In this case, either use cat
:
var=$( cat somefile 2>/dev/null )
or test for file existence first:
if [ -f somefile ]; then
var=$(<somefile)
fi
To avoid the race condition in the last example (the file might theoretically disappear between the test and reading it):
if cp somefile myname 2>/dev/null; then
var=$(<myname)
rm -f myname
fi
The following will not work:
var=$(<somefile) 2>/dev/null
var=$(<somefile 2>/dev/null)
This is the pettiest gripe, but the second fix has a race condition...
â Michael Homer
Mar 14 at 9:04
@MichaelHomer It has. The file might theoretically disappear between the test and reading it. Another way would be to copy the file and read it if that succeeded, but thecat
is probably best (and also portable).
â Kusalananda
Mar 14 at 9:25
add a comment |Â
up vote
4
down vote
In this case, either use cat
:
var=$( cat somefile 2>/dev/null )
or test for file existence first:
if [ -f somefile ]; then
var=$(<somefile)
fi
To avoid the race condition in the last example (the file might theoretically disappear between the test and reading it):
if cp somefile myname 2>/dev/null; then
var=$(<myname)
rm -f myname
fi
The following will not work:
var=$(<somefile) 2>/dev/null
var=$(<somefile 2>/dev/null)
This is the pettiest gripe, but the second fix has a race condition...
â Michael Homer
Mar 14 at 9:04
@MichaelHomer It has. The file might theoretically disappear between the test and reading it. Another way would be to copy the file and read it if that succeeded, but thecat
is probably best (and also portable).
â Kusalananda
Mar 14 at 9:25
add a comment |Â
up vote
4
down vote
up vote
4
down vote
In this case, either use cat
:
var=$( cat somefile 2>/dev/null )
or test for file existence first:
if [ -f somefile ]; then
var=$(<somefile)
fi
To avoid the race condition in the last example (the file might theoretically disappear between the test and reading it):
if cp somefile myname 2>/dev/null; then
var=$(<myname)
rm -f myname
fi
The following will not work:
var=$(<somefile) 2>/dev/null
var=$(<somefile 2>/dev/null)
In this case, either use cat
:
var=$( cat somefile 2>/dev/null )
or test for file existence first:
if [ -f somefile ]; then
var=$(<somefile)
fi
To avoid the race condition in the last example (the file might theoretically disappear between the test and reading it):
if cp somefile myname 2>/dev/null; then
var=$(<myname)
rm -f myname
fi
The following will not work:
var=$(<somefile) 2>/dev/null
var=$(<somefile 2>/dev/null)
edited Mar 14 at 10:05
answered Mar 14 at 8:48
Kusalananda
103k13201318
103k13201318
This is the pettiest gripe, but the second fix has a race condition...
â Michael Homer
Mar 14 at 9:04
@MichaelHomer It has. The file might theoretically disappear between the test and reading it. Another way would be to copy the file and read it if that succeeded, but thecat
is probably best (and also portable).
â Kusalananda
Mar 14 at 9:25
add a comment |Â
This is the pettiest gripe, but the second fix has a race condition...
â Michael Homer
Mar 14 at 9:04
@MichaelHomer It has. The file might theoretically disappear between the test and reading it. Another way would be to copy the file and read it if that succeeded, but thecat
is probably best (and also portable).
â Kusalananda
Mar 14 at 9:25
This is the pettiest gripe, but the second fix has a race condition...
â Michael Homer
Mar 14 at 9:04
This is the pettiest gripe, but the second fix has a race condition...
â Michael Homer
Mar 14 at 9:04
@MichaelHomer It has. The file might theoretically disappear between the test and reading it. Another way would be to copy the file and read it if that succeeded, but the
cat
is probably best (and also portable).â Kusalananda
Mar 14 at 9:25
@MichaelHomer It has. The file might theoretically disappear between the test and reading it. Another way would be to copy the file and read it if that succeeded, but the
cat
is probably best (and also portable).â Kusalananda
Mar 14 at 9:25
add a comment |Â
up vote
2
down vote
You can use:
var=$(<somefile); 2> /dev/null
Hmm... yes, that would be best. Didn't we have this in an answer the other day (I can't find it)?
â Kusalananda
Mar 14 at 10:28
@Kusalananda, yes, I couldn't find it either, but I'm pretty sure there's a recent dup too.
â Stéphane Chazelas
Mar 14 at 10:35
add a comment |Â
up vote
2
down vote
You can use:
var=$(<somefile); 2> /dev/null
Hmm... yes, that would be best. Didn't we have this in an answer the other day (I can't find it)?
â Kusalananda
Mar 14 at 10:28
@Kusalananda, yes, I couldn't find it either, but I'm pretty sure there's a recent dup too.
â Stéphane Chazelas
Mar 14 at 10:35
add a comment |Â
up vote
2
down vote
up vote
2
down vote
You can use:
var=$(<somefile); 2> /dev/null
You can use:
var=$(<somefile); 2> /dev/null
answered Mar 14 at 10:22
Stéphane Chazelas
280k53515847
280k53515847
Hmm... yes, that would be best. Didn't we have this in an answer the other day (I can't find it)?
â Kusalananda
Mar 14 at 10:28
@Kusalananda, yes, I couldn't find it either, but I'm pretty sure there's a recent dup too.
â Stéphane Chazelas
Mar 14 at 10:35
add a comment |Â
Hmm... yes, that would be best. Didn't we have this in an answer the other day (I can't find it)?
â Kusalananda
Mar 14 at 10:28
@Kusalananda, yes, I couldn't find it either, but I'm pretty sure there's a recent dup too.
â Stéphane Chazelas
Mar 14 at 10:35
Hmm... yes, that would be best. Didn't we have this in an answer the other day (I can't find it)?
â Kusalananda
Mar 14 at 10:28
Hmm... yes, that would be best. Didn't we have this in an answer the other day (I can't find it)?
â Kusalananda
Mar 14 at 10:28
@Kusalananda, yes, I couldn't find it either, but I'm pretty sure there's a recent dup too.
â Stéphane Chazelas
Mar 14 at 10:35
@Kusalananda, yes, I couldn't find it either, but I'm pretty sure there's a recent dup too.
â Stéphane Chazelas
Mar 14 at 10:35
add a comment |Â