2

I have a .sh file and I want to execute it from shell without writing the extension. What I did:

  • I created a directory and added it to $PATH
  • I gave to the file.sh chmod 711
  • and the file contain #!/bin/sh (I tried also bash).

However when I try to execute myscript without sh I get command not found while if I try with myscript.sh I get the right result.

How could I do?

I read also: How to run a shell script on a Unix console or Mac terminal? and executing shell script without calling sh implicitly but no solution

Result of ls -l

ls -l /Users/Mitro/scripts
total 8
-rwx--x--x  1 Mitro  staff  22 Nov 26 10:25 myscript.sh

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Users/Mitro/scripts
Community
  • 1
  • 1
Mitro
  • 1,230
  • 8
  • 32
  • 61
  • What is the exact command you are trying? It's unclear what you mean. – l'L'l Nov 26 '15 at 09:47
  • I've written a sh file that just write an echo, I want ececute it by typing myscript and not nmp.sh – Mitro Nov 26 '15 at 10:54
  • the permissions should probably be 755 so that everyone has read and execute permissions. It doesn't do much good to give someone execute permissions without being able to read the file from disk. – onlynone Dec 01 '15 at 20:24

3 Answers3

2

Two problems...

Firstly, file is already an executable program in /usr/bin that tells you the type of a file - i.e. whether it is an image, or a song or a database. Quick example:

file a.png
a.png: PNG image data, 1 x 1, 1-bit colormap, non-interlaced

So, file is a bad name for a shell script - likewise is test.

Secondly, if you want to execute a script or program in your current directory, also known as dot (.), you either need to have dot in your PATH, or you need to explicitly tell your shell that the file you want to run is in the current directory. The easier option is the second, which means if your script is called fred, you run it with

./fred

which tells the shell it is in your current directory.

The longer option, if you want to always be able to run scripts in the current directory, is to add dot to your PATH. So, you locate your login script (probably $HOME/.profile) and you find the line that sets your PATH and you add the current directory to it.

export PATH=$PATH:.

Once you have set that, you are best off logging out and back in to have it take effect.

Some folks disapprove of the idea of adding dot to their PATH - I don't. YMMV.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Name file is just an example, sorry for being ambigue – Mitro Nov 26 '15 at 10:30
  • 1
    Ok, please click `edit` under your question and paste the output of `ls -l /full/path/to/your/script` and also the output of `echo $PATH` and also the *exact* command you tried to un. – Mark Setchell Nov 26 '15 at 10:39
  • 2
    Ok. If you want to run the script by typing `myscript` in the Terminal, you need to rename the script, like this `cd /Users/Mitro/scripts; mv myscript.sh myscript` Then, as you have changed the name, you need to tell your shell to rebuild its list of commands using `hash -r`and then you can run it with `myscript`. – Mark Setchell Nov 26 '15 at 10:59
  • Thank you, this work. I thought it was necessary to have the extension. However I didn't use hash -r, what do this command should do exactly? – Mitro Nov 26 '15 at 11:04
  • 1
    It looks at all the directories listed in your `PATH` and makes a list, **in memory**, of all the executable programs on your `PATH`. Then, when you type any command, it doesn't have to look (on disk) at all the directories in your `PATH` because it already *"knows"* where all the programs are. – Mark Setchell Nov 26 '15 at 11:07
1

You can add alias. If you have /some/path/to/script.py, do:

alias my_script='/some/path/to/my_script.py'

Now when you enter my_script, your script would be executed.

alnet
  • 1,093
  • 1
  • 12
  • 24
0

For mac the profile file is ~/.bash_profile as opposed to ~/.profile which did not work.

Ergun Coruh
  • 385
  • 3
  • 16