-1

Hi my bash script says the that the first parameter passed into it doesn't exist as a file but it does. Please what have I done wrong?

to run the script and pass in two arguments from linux: ./concantenatefile.sh file1.txt file2.txt

#!/bin/bash

file1="$1"

if [ -e file1 ]
then    
    echo "The file $file1 exists"
else    
    echo "The file $file1 doesn't exist"
fi

It says The file file1.txt doesn't exist However there is a file called file1.txt in the present working directory.

1
  • 1
    if [ -e $file1 ] - you missed the $ sigil before the variable name.
    – Tanktalus
    Commented Apr 2, 2019 at 0:00

1 Answer 1

-1

you are not currently derefencing the file1 variable In the line

if [ -e file1 ]

You need to add a $ to derefence the variable and access its content as so

if [ -e $file1 ]

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