0

I keep getting a "command not found" error after checking a variable for the substring .txt.

Here's a simple version of my script.

myscript.sh

#!/bin/sh
if [["$1" == *.txt]]
    then
    echo $1
fi

Result:

> ./myscript.sh argument.txt
./myscript.sh: line 2: [[argument.txt: command not found]]
Ryan
  • 14,115
  • 29
  • 92
  • 163
  • #meta, is it advisable to delete questions like these? I obviously didn't know it was a simple typo issue when I wrote the question. Is it better to leave this up for others to discover the mistake themselves or delete the question entirely. – Ryan Apr 01 '14 at 16:17
  • duplicate of http://stackoverflow.com/questions/19733437/if-compare-strings-get-a-command-not-found-error which seems like a much worse 'question' than this. – KymikoLoco Oct 28 '14 at 19:48

1 Answers1

3

The error is because of a space needed around the brackets [[ and ]]:

#!/bin/sh
if [[ "$1" == *.txt ]]
    then
    echo $1
fi

That is, instead of:

if [["$1" == *.txt]]

use

if [[ "$1" == *.txt ]]
fedorqui 'SO stop harming'
  • 228,878
  • 81
  • 465
  • 523