简体   繁体   中英

How to execute a file without .sh extension in shell

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"

If the file is already executable as abc.sh , then all you need to do is

mv abc.sh abc

(assuming you are in the directory where the file lives)

In a Linux or Unix shell, file extension doesn't affect whether it will execute or not.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM