Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

executing shell script without calling sh implicitly

Tags:

I was wondering if it is possible to make a "link" in usr/bin (i.e.) that leads to a shell-script.

But I want just to write

% shellscript 

instead of

% sh shellscript.sh 

kinda like an alias.

Is this possible?

like image 951
ShoX Avatar asked Jun 08 '10 16:06

ShoX


People also ask

Is it possible to execute a script without execution permissions?

For shell scripts without execute permissions, you will need to pass the script as an argument to your choice of interpreter. Here the execute permissions are checked for your shell interpreter and not the script itself. So you should have execute for /bin/sh and your script can have only read permissions.

How can you run a Bash script if you do not have execute permissions on the script?

As long as bash is executable, you can always run bash with the script file as argument, or run bash interactively and copy paste the script line by line into your terminal to have the commands executed.


1 Answers

Make the first line of the script

#!/bin/sh 

Then make it executable by typing the command:

chmod +x shellscript.sh 

If you now place the script in a bin folder that is on your system's PATH variable and you will be able to run it directly. To see the folders in your path, type:

echo $PATH 

I usually use /home/[my username]/bin for scripts that I have written so that they don't interfere with other users on the system. If I want them to be for all users, I use /usr/local/bin which is supplied empty on most distributions.

The .sh on the end of the script's filename is only a convention to help you remember what kind of file it is. It will still work if you rename it to just shellscript, for example, which will complete your requirements.

like image 95
rjmunro Avatar answered Nov 11 '22 17:11

rjmunro