‘-‘ is actually a special symbol that helps to signal the end of a file with a lot of binary content. Files with this symbol are regular files, not actual binaries or programs.
You can easily tell a file like this is a binary file from the name alone, as I did in the example above, and we can do that with Bash.
The trick is, is to do a way to find out the type of file, so Bash can work with the type (it could be a program, a binary, an archive, etc.) and in your case if the type is in fact binary, then you use ‘-‘ to indicate the end of the file, as above.
This implies that even when you read the file line by line, Bash will know that the file is a binary one as it doesn’t even think about it. It’s also there are many binaries that can change their name to ‘-‘ to not show it as a binary when it’s actually a binary. If a file that’s name started with ‘-‘ is a binary, then you’d need to think twice about it (e.g. you’d want to check it for the ‘-‘ symbol before loading the file line by line).
So, to accomplish what you want to do, you just need to use some Bash expression to decide if a file type is binary or not.
You can use the Bash extglob flag (the -ext option) or a regular expression that allows you to search for this binary pattern. It’s called:
‘[ -~][-.]*’ – [[:~:]] matches an optional ‘-‘ or ‘.’ character then the rest of the sequence matches any character.
So, if your file name starts with ‘-‘ then it is considered a binary name.
If you just want to see if the file name you get is ‘-‘ then you can use Bash expr, which is a really neat and compact Bash utility.
Tested on bash.exe -v
The following tests are done on the MinGW shell (W.H. Guo).
If we have a file with name that starts with ‘-‘ and ends in ‘.exe’, then it is considered binary:
$ cat file1 –