I write a bash file in which i used read command to read data from a file.
If the file wasn’t there I want to save the error into a text file. I tried:
read myVariable < myFile 2> errorFile.txt
it doesn’t work, and many other efforts faild such as:
myVar=`read myVariable < myFile`
You need to redirect STDERR first before you tell bash to read from a file that doesn’t exist
This will work for you:
or