0

This question already has an answer here:

I am trying to run a script like :

./script.sh file

but I am receiving ( if I use a txt file ):

=:        cannot open `=' (No such file or directory)
test.txt: ASCII text
Second

If I use a gz file:

=:           cannot open `=' (No such file or directory)
test.txt.gz: gzip compressed data, was "test.txt", last modified: Wed Jul 20 09:17:58 2016, from Unix
Second

( I have the script and the file in the same directory )

script:

#!/bin/bash

file = $1

if [[ $file == *.gz ]];then

    echo "First"
else
    echo "Second"

fi

marked as duplicate by Gilles shell-script Jul 20 '16 at 21:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    @Rahul irrelevant. The problem is spaces around = – 123 Jul 20 '16 at 9:36
5
file = $1

runs the file command with = as the first argument and the result of the split+glob operator applied to the script's first parameter as the remaining arguments.

Variable assignments in Bourne-like shells (like bash, ksh, zsh, ash/dash, yash) are with no spaces around the = sign:

file=$1

file = $1 would be valid as an assignment in rc, es or akanga shells. csh and tcsh have yet another syntax: set file = $1:q and fish uses set file $argv[1].

Not the answer you're looking for? Browse other questions tagged or ask your own question.