I am under the impression that if I include the line
#!/bin/bash
at the start of my file, it will be recognized as a bash script that I can run just by
file.sh
instead of
sh file.sh
But in my case, file.sh
gives something like test.sh
gives test.sh: command not found
. Why is that?
UPDATE
I notice I need to use ./
. But I thought it will look in the current directory anyway?
./test.sh
First you need to make sure the file is executable:
chmod +x file.sh
And then to run it, you either need to put it in your PATH, i.e., one of the directories of files the OS searches when looking for files to run, which you can find with
echo $PATH
, or else you need to type in:./file.sh
Rather than just type
file.sh
.You can just type
file.sh
, if it's in your PATH, which I recommend. A good place is in~/bin
. Create that folder if it's not there, and under Ubuntu, it'll get added to your path when you log in.You need to start it with ./file.sh, because the current dir is not in the PATH.
If this isn't sufficient, becaused you missed, what frabjous suggests, to
chmod a+x
, you should start programs beginning with a shebang#!/bin/bash
withnot
even if sh is a symbolic link to /bin/bash.
The shell checks how it was invoked, and can be invoked as
sh
to act in a compatible mode, therefore it can fail to handle some bashisms, which would work if invoked asbash file.sh
.That was not your problem, but might become one, if you don't know these subtle distinction.