So I got a .sh file. When I double click it opens it in gui text editor that look like vi text editor instead of the Linux terminal. How can I open .sh file in terminal and run it? What is the command to run .sh file on Linux?
Check the file permissions. Make sure you have execute (if not, chmod +x filename) and the use ./filename:
chmod +x filename
./filename
If you do nothing to a shell script, in terms of permissions, you can execute it by using:
sh ./my_new_script.sh
and as Nademan said, once the mode is changed for execution, then just use
./my_new_script.sh
You may want to get a better understanding of permissions before you start changing them.
Unix has three logical groups - the owner, the group, and everyone else. Permissions not only allow things to be done, but also prevent settings from being created on objects.
Want to see the detailed permissions of a directory? Use:
root# ls -la
total 3112 dr-xr-x---. 31 root root 4096 Jan 16 15:04 . dr-xr-xr-x. 18 root root 4096 Aug 21 23:24 .. -rw-------. 1 root root 1771 Sep 12 2016 anaconda-ks.cfg -rw-------. 1 root root 11808 Jan 16 13:54 .bash_history -rw-r--r--. 1 root root 18 Dec 28 2013 .bash_logout -rw-r--r--. 1 root root 176 Dec 28 2013 .bash_profile -rwxr-xr-x. 1 root root 1409 Feb 22 2018 .bashrc drwxr-xr-x. 2 root root 4096 Jan 22 2018 bin
The only “executable” in this list is “.bashrc”, my resource file, used when I run a bash shell.
The rest are “644” or -rw-r–r-- Meaning: read and write for the owner, read only for the group, and read only for everyone else. The octal equivalents with the letters interchangeably with the chmod commands. It gets a bit involved when people start talking about sticky bits, setuid, setgid, and unless you need to force new files to be owned by a specific person or group, or always have a certain permission setting, then it’d be overkill to learn about that right now.
Method 1
Command to run .sh file using GUI (desktop) method
- Select the file using mouse.
- Right-click on the file.
- Choose Properties :
- Click the Permissions tab.
- Select Allow executing file as a program on Linux:
- Close the box.
- Finally, double-click the file foo.sh. A dialog will open and ask you to run the script in a Linux terminal.
Method 2
Chmod method is easy for sure. Open the terminal window. Navigate to folder/dir where foo.sh is stored (say in $HOME/Downloads directory)
cd ~/Downloads
Now run:
ls -l foo.sh
chmod +x foo.sh
./foo.sh
/path/to/foo.sh
ls -l foo.sh
Also see
https://www.cyberciti.biz/faq/run-execute-sh-shell-script/