Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a file without .sh extension in shell

Tags:

linux

shell

unix

sh

I want to execute a file in bash without the .sh extension.

Example: I have file "abc.sh" which I can execute directly (as I have added #!/bin/bash as the first line) but I want the filename to be just "abc"

like image 621
Sai Kiran Avatar asked Nov 21 '16 13:11

Sai Kiran


People also ask

Is .sh extension necessary?

Executables should have no extension (strongly preferred) or a . sh extension. Libraries must have a . sh extension and should not be executable.

How do I run a .sh file in Windows without Bash?

1] Execute Shell Script file using WSL And search for “Windows Features”, choose “Turn Windows features on or off”. Scroll to find WSL, check the box, and then install it. Once done, one has to reboot to finish installing the requested changes. Press Restart now.


2 Answers

What are the permissions on the file? To make it executable with doing something like ./abc.sh it needs to have EXECUTABLE rights.

You can always do bash abc.sh

Linux permissions overview

Filename in Linux doesn't mean anything in terms of execution capabilities, you can call the file myfile.something.something and it can still be executable. You can name it abc but it has to have EXECUTABLE rights for the user,group,other.

To add that permission you can do chmod +x <filename> but you should look at the link above for a better understanding.

like image 141
ioneyed Avatar answered Oct 04 '22 10:10

ioneyed


In Linux you use ./filename too run a script. And you need execute permission:

chmod 755 filename

But you still need the "Shebang":

#!/bin/bash

From here I got this:

If you did not put the scripts directory in your PATH, and . (the current directory) is not in the PATH either, you can activate the script like this:

./script_name.sh

A script can also explicitly be executed by a given shell, but generally we only do this if we want to obtain special behavior, such as checking if the script works with another shell or printing traces for debugging:

rbash script_name.sh

sh script_name.sh

bash -x script_name.sh

like image 37
WasteD Avatar answered Oct 04 '22 11:10

WasteD