0
#!/bin/bash
echo "Unesite argument:$1 "
var=$1

if [[ ! -f "$var" ]]
then
        touch $var
        sleep 1
        echo "Nova datoteka $var je kreirana."
else
        echo "Ova datoteka vec postoji, unesite drugu."
        sleep 1
fi

Why do I get "touch missing file operand"

  • please don't post pictures of text. I can't read this, select parts of it, let any tools run on it… Instead, simply copy&paste the text, select it, and click on the "code formatting" button labeled {} in the question editor. – Marcus Müller Dec 02 '22 at 19:09
  • 2
    so, have you checked what the content of var actually is? – Marcus Müller Dec 02 '22 at 19:11

1 Answers1

2

You didn't provide the script with an argument. As a result, both $1 and $var expand to the empty string.

The empty string is certainly not the name of a file that exists, so touch $var is executed. Because you didn't quote the expansion, it's equivalent to touch with no argument at all.

If you had quoted $var, you'd execute touch "", which would give you a different error message, along the lines of

touch: : No such file or directory

because again, the empty string is not a legal file name.

chepner
  • 7,501