This is very strange indeed. The steps you describe should have worked, so there must be some small error in your environment or the execution of those steps. There are some things you can do to help diagnose this:
CHECKING FOR CONTROL CHARACTERS
What you've documented looks fine, as long as there are no typos or control characters. You should check by typing:
cat -vt ./test
If you see any unexpected extra text that could explain the problem. For example, "^M" at the end of a line would indicate that your editor saved the file in Windows format.
REGENERATING THE FILE RELIABLY
To create a known-good ./test2
, copy-paste the commands below:
`which bash`
printf "#\!`which sh`\necho HELLO\n" > ./test2
chmod +x ./test2
./test2
exit
CHECKING WHICH COMMAND CAN NOT BE FOUND
If you type...
./ajio
...do you get exactly...
./ajio: Command not found.
...as you described with ./test
? I just made up the name ajio so it shouldn't exist. If they messages match then it doesn't really tell you anything new. But if the messages differ, that confirms that ./test
was at least found and executable.
It's also possible that your version of sh
is trying to tell you not that test
can't be found, but that while running test
some command the shell tried to run couldn't be found. That shouldn't be the echo command, as with most shell implementations that will be an internal command, implemented inside the shell. But, the shell may run an initialisation script containing a line specifying a command it can't run. If you run man sh
, it will tell you about all the different startup files that your shell might try to run. These can be different from those used when you start the shell interactively. But, as a beginner, it can be daunting checking the validity of those scripts. It's likely that any bogus customisations would be to your personal shell startup process though, and not afflicting the entire Linux install, so run ls -ld ~/.*
to list the hidden files in your home directory, and inspect any that look like shell startup files (e.g. ~/.bashrc, ~/.profile, ~/.bash_login). Check any commands they specify can be found with which, and are invoked after the path variable is set to include their location.
COMPARING TO ANOTHER SHELL
If there's a problem with the /bin/sh install / initialisation, then you might be able to bypass it by invoking another shell. Try...
which zsh
which tcsh
which csh
...and if one of more of these finds an alternative shell for you, edit or recreate the file specifying that shell, ala...
#!/bin/csh
echo HELLO
...then chmod +x
it and ./
-run it. If that works, then you know your problem is /bin/sh specific.