Views:
2,028,408β
Votes: 2β
Tags:
command-line
executable
Link:
π See Original Answer on Ask Ubuntu β§ π
URL:
https://askubuntu.com/q/1364586
Title:
How to make a file (e.g. a .sh script) executable, so it can be run from a terminal
ID:
/2021/09/19/How-to-make-a-file-_e.g.-a-.sh-script_-executable_-so-it-can-be-run-from-a-terminal
Created:
September 19, 2021
Upload:
April 21, 2025
Layout: post
TOC:
false
Navigation: false
Copy to clipboard: false
Let all users run your script
As stated you can set the execution bit of the file to make it executable with chmod +x
. But you can also use chmod a+x
:
$ ll file_command.txt
-rw-rw-r-- 1 rick rick 17 Sep 19 08:55 file_command.txt
$ chmod a+x file_command.txt
$ ll file_command.txt
-rwxrwxr-x 1 rick rick 17 Sep 19 08:55 file_command.txt*
NOTES:
- A script doesnβt have to end with
.sh
it can end in.txt
as shown here, or have no extension whatsoever. - Instead of just
+x
(only you can execute the script), usea+x
so all users can execute the script. - The file name has a
*
appended to it to indicate it is executable. Also the file name changes color to green on most systems.
Run a script without making it executable
You can still run a bash script without making it executable. For example:
$ echo "echo Hello World" > file_command.txt
$ cat file_command.txt
echo Hello World
$ bash file_command.txt
Hello World
NOTES:
- First we create a script containing
echo Hello World
. - Then we confirm the script called
file_command.txt
is correct. - Lastly we run the script by calling
bash
and passing it the script name. Thebash
command is actually store as/bin/bash
and it is an executable on all Ubuntu systems. - Creating a file of commands saves you from adding the shebang
#!/bin/bash
as the first line in a script. -
Creating a file of commands saves you from making it executable with
chmod +x
orchmod a+x
.