0

I have a file named "file" in my current directory. I want to check the type of this using file command and variables. I created this script:

name="file"
file $name

but it is giving this error:

: cannot open `file\015\015' (No such file or directory)
Rembo
  • 43
  • 1
  • 9
  • `\015` is the octal value of carriage return. So you need to remove the carriage returns from the `$name` variable. – builder-7000 Feb 15 '20 at 21:01
  • How can I do that? – Rembo Feb 15 '20 at 21:06
  • or just remove the cr's before doing anything in it on Unix like system. hint `dos2unix` is the easiest if available. – Jetchisel Feb 15 '20 at 21:22
  • Generally, avoid using a Windows editor to edit scripts, or figure out how to configure it to save these files with correct line endings for the destination platform. – tripleee Feb 16 '20 at 08:39

1 Answers1

-3

I think the problem is that you named your file as file.

So rename your file as file2, then

name="file2"
file $name

Explanation

The problem with the name file is bash is trying to RUN your file against itself, when you have your current directory in your PATH.

Philippe
  • 20,025
  • 2
  • 23
  • 32
  • Are you sure about that? `echo bar > file; name=file; file "$name"` the output `file: ASCII text` – Jetchisel Feb 15 '20 at 21:54
  • If you put 2 CR in your `file`, what should you get ? I bet again its "ASCII text" – Philippe Feb 15 '20 at 22:06
  • So whats the point on renaming `file` to `file2`? – Jetchisel Feb 15 '20 at 22:08
  • And to answer that question: `printf 'DOS\r\nline endings\r\n' > file; name=file; file "$name"` output: `file: ASCII text, with CRLF line terminators` – Jetchisel Feb 15 '20 at 22:10
  • That's not what is happening. Whatever you put after a command name is passed as an argument to that command. Except if variable expansions or shell expansions are found, in which case they get evaluated before being converted into parameters by the shell. However, nothing special is going here. – Luis Lavaire. Feb 15 '20 at 22:21
  • jfyi, It is not my down vote, I tried to explain that to you... – Jetchisel Feb 15 '20 at 22:21