File1: Binary operator expected

I am trying to create a shell script as below on Linux.

#!/bin/bash
if [ cp file1 /tmp ];
then
  rm file1
fi

But I am getting an error message as "file1:binary operator required"
I have tried a lot of options like enclosing line 2 within [[ ]], () etc but it’s not working. I am not sure what am I missing. I even tried searching for other similar questions on StackOverflow but none are solving my problem.

So can you please help?

>Solution :

You should write:

#!/bin/bash
if cp file1 /tmp ; then
  rm file1
fi

Or

cp file1 /tmp && rm file1

Or perhaps directly:

mv file1 /tmp

Leave a Reply