How to run the script in linux

How To Run a Script In Linux

H ow do I run a Linux shell script? How can I run a script in Linux operating system using command line options?

By default, the shell script will not run. You need to set execute permission for your shell script. To execute or run script type the following command:

Tutorial details
Difficulty level Easy
Root privileges No
Requirements None
Est. reading time N/A

chmod +x script-name-here
OR
chmod 0755 script.sh
Next, use the ls command to view permission on the script:
$ ls -l script-name-here

To execute the script, type:
$ ./script-name-here

You can also run a script using any one of the following syntax:
$ /path/to/shell/script/backup.sh

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Run a script called backup.ksh using ksh shell:
$ ksh backup.ksh

To run a script called backup.bash using BASH shell:
$ bash backup.bash

Examples that shows how to run a script in Linux

Create a shell script called hello.sh using a text editor such as vi or gedit/nano:
nano hello.sh
OR
vim hello.sh
Append the following code:

Conclusion

You learned how to write a simple shell script and run a script in Linux operating system with help of chmod and other commands. Please see the following tutorials for more information on bash shell scripting under Linux or Unix-like operating systems:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

this is very good. It’s just missing one thing. the export command should be added in in the

/.bashrc file to survive across sessions.
$ tail -1

or the equivalent for your chosen shell (bs, ksh, tcsh etc …)

Just the solution I needed to figure this out, thanks man! 😀

Nice.
For compiling C prgms,
cc hello.c
./a.out

btw, Why don’t you have categories in WP?

Click on category icon (hint: Bash shell).

Hello Vivek, I was not aware that that image is a category link, too 🙂 It really looks like a post image. I’m referencing this site from time to time, and I’ve learned that you have categories just now! I’d really suggest using a different method for showing categories, unless you want to hide them 🙂 Thanks for the great site.

we can run a script even with out permissions using source command
sourch scriptname.sh

no dear if u want to run script the u hv to give execute permission on that file

like….scriptname.sh
chmod 755 or 777 scriptname.sh

Don’t be a patronizing shit…

hi. i m facing problem to write for setup ip address ,subnet and gateway in linux

kindly suggest me how i do…
if u have the give me idea…

#include
int main()
<
system(“c:\\windows\\your system name\\ip config”);
return 0;
>

Good information. I am trying to run a sql using a shell command.

Please provide the info..

How to run the Unix command only during the even and odd hours only.

Kindly provide the command to incorporate in the script.

Hi,
I am having a problem related to this. I have downloaded an program and it has a GUI written in Java. I need to run the script to launch this program, with “sudo” privileges in order to having it running properly. I don’t want to navigate to the folder where this script is, every time I need it, therefore I first made it executable and added the folder to the PATH. Now, when I write “sudo script-name”, I get “script-name not command found”, if I write only “script-name”, it finds it but it doesn’t run properly. Is there a way to launch a script, that is in the PATH, with sudo privileges? Thank you in advance.

Run it as follows:

Or cd to /home/foo and run:

Thank you. I decide to add it as an alias: “alias script-name=’sudo bash /path/to/script/script-name”

What’s the difference with §. /PATH/TO/TARGET§ and §./PATH/TO/TARGET§ ?

thanx, im learning and your are a good teacher.

Источник

How to Run a Shell Script in Linux [Essentials Explained for Beginners]

Last updated July 19, 2021 By Abhishek Prakash 18 Comments

There are two ways to run a shell script in Linux. You can use:

Or you can execute the shell script like this:

That maybe simple, but it doesn’t explain a lot. Don’t worry, I’ll do the necessary explaining with examples so that you understand why a particular syntax is used in the given format while running a shell script.

I am going to use this one line shell script to make things as uncomplicated as possible:

Method 1: Running a shell script by passing the file as argument to shell

The first method involves passing the script file name as an argument to the shell.

Considering that bash is the default shell, you can run a script like this:

Do you know the advantage of this approach? Your script doesn’t need to have the execute permission. Pretty handy for quick and simple tasks.

» data-medium-file=»https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/run-a-shell-script-linux.png?fit=300%2C133&ssl=1″ data-large-file=»https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/run-a-shell-script-linux.png?fit=741%2C329&ssl=1″ loading=»lazy» width=»741″ height=»329″ src=»https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/run-a-shell-script-linux.png?resize=741%2C329&ssl=1″ alt=»Run A Shell Script Linux» srcset=»https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/run-a-shell-script-linux.png?w=741&ssl=1 741w, https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/run-a-shell-script-linux.png?resize=300%2C133&ssl=1 300w, https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/run-a-shell-script-linux.png?resize=100%2C44&ssl=1 100w, https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/run-a-shell-script-linux.png?resize=150%2C67&ssl=1 150w» sizes=»(max-width: 741px) 100vw, 741px» data-recalc-dims=»1″/> Running a Shell Script Linux

If you are not familiar already, I advise you to read my detailed guide on file permission in Linux.

Keep in mind that it needs to be a shell script that you pass as argument. A shell script is composed of commands. If you use a normal text file, it will complain about incorrect commands.

» data-medium-file=»https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/running-text-file-as-script.png?fit=300%2C133&ssl=1″ data-large-file=»https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/running-text-file-as-script.png?fit=741%2C329&ssl=1″ loading=»lazy» width=»741″ height=»329″ src=»https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/running-text-file-as-script.png?resize=741%2C329&ssl=1″ alt=»Running Text File As Script in Linux» srcset=»https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/running-text-file-as-script.png?w=741&ssl=1 741w, https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/running-text-file-as-script.png?resize=300%2C133&ssl=1 300w, https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/running-text-file-as-script.png?resize=100%2C44&ssl=1 100w, https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/running-text-file-as-script.png?resize=150%2C67&ssl=1 150w» sizes=»(max-width: 741px) 100vw, 741px» data-recalc-dims=»1″/> Running a Text File As Script

In this approach, you explicitly specified that you want to use bash as the interpreter for the script.

Shell is just a program and bash is an implementation of that. There are other such shells program like ksh, zsh, etc. If you have other shells installed, you can use that as well instead of bash.

For example, I installed zsh and used it to run the same script:

» data-medium-file=»https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/01/execute-shell-script-with-zsh.png?fit=300%2C102&ssl=1″ data-large-file=»https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/01/execute-shell-script-with-zsh.png?fit=741%2C253&ssl=1″ loading=»lazy» width=»741″ height=»253″ src=»https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/01/execute-shell-script-with-zsh.png?resize=741%2C253&ssl=1″ alt=»Execute Shell Script With Zsh» srcset=»https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/01/execute-shell-script-with-zsh.png?w=741&ssl=1 741w, https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/01/execute-shell-script-with-zsh.png?resize=300%2C102&ssl=1 300w, https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/01/execute-shell-script-with-zsh.png?resize=100%2C34&ssl=1 100w, https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/01/execute-shell-script-with-zsh.png?resize=150%2C51&ssl=1 150w» sizes=»(max-width: 741px) 100vw, 741px» data-recalc-dims=»1″/> Execute Shell Script With Zsh

Recommended Read:

How to Run Multiple Linux Commands at Once in Linux Terminal [Essential Beginners Tip]

Method 2: Execute shell script by specifying its path

The other method to run a shell script is by providing its path. But for that to be possible, your file must be executable. Otherwise, you’ll have “permission denied” error when you try to execute the script.

So first you need to make sure that your script has the execute permission. You can use the chmod command to give yourself this permission like this:

Once your script is executable, all you need to do is to type the file name along with its absolute or relative path. Most often you are in the same directory so you just use it like this:

If you are not in the same directory as your script, you can specify it the absolute or relative path to the script:

» data-medium-file=»https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/01/running-shell-script-in-other-directory.png?fit=300%2C103&ssl=1″ data-large-file=»https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/01/running-shell-script-in-other-directory.png?fit=795%2C272&ssl=1″ loading=»lazy» width=»795″ height=»272″ src=»https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/01/running-shell-script-in-other-directory.png?resize=795%2C272&ssl=1″ alt=»Running Shell Script In Other Directory» srcset=»https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/01/running-shell-script-in-other-directory.png?w=795&ssl=1 795w, https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/01/running-shell-script-in-other-directory.png?resize=300%2C103&ssl=1 300w, https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/01/running-shell-script-in-other-directory.png?resize=768%2C263&ssl=1 768w, https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/01/running-shell-script-in-other-directory.png?resize=100%2C34&ssl=1 100w, https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/01/running-shell-script-in-other-directory.png?resize=150%2C51&ssl=1 150w» sizes=»(max-width: 795px) 100vw, 795px» data-recalc-dims=»1″/> Running Shell Script In Other Directory

That ./ before the script is important (when you are in the same directory as the script)

» data-medium-file=»https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/executing-shell-scripts-linux.png?fit=300%2C169&ssl=1″ data-large-file=»https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/executing-shell-scripts-linux.png?fit=800%2C450&ssl=1″ loading=»lazy» width=»800″ height=»450″ src=»https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/executing-shell-scripts-linux.png?resize=800%2C450&ssl=1″ alt=»Executing Shell Scripts Linux» srcset=»https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/executing-shell-scripts-linux.png?w=800&ssl=1 800w, https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/executing-shell-scripts-linux.png?resize=300%2C169&ssl=1 300w, https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/executing-shell-scripts-linux.png?resize=768%2C432&ssl=1 768w, https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/executing-shell-scripts-linux.png?resize=100%2C56&ssl=1 100w, https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/executing-shell-scripts-linux.png?resize=150%2C84&ssl=1 150w» sizes=»(max-width: 800px) 100vw, 800px» data-recalc-dims=»1″/>

Why can you not use the script name when you are in the same directory? That is because your Linux systems looks for the executables to run in a few selected directories that are specified in the PATH variable.

Here’s the value of PATH variable for my system:

This means that any file with execute permissions in one of the following directories can be executed from anywhere in the system:

The binaries or executable files for Linux commands like ls, cat etc are located in one of those directories. This is why you are able to run these commands from anywhere on your system just by using their names. See, the ls command is located in /usr/bin directory.

» data-medium-file=»https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/locating-command-linux.png?fit=300%2C103&ssl=1″ data-large-file=»https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/locating-command-linux.png?fit=795%2C272&ssl=1″ loading=»lazy» width=»795″ height=»272″ src=»https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/locating-command-linux.png?resize=795%2C272&ssl=1″ alt=»Locating Command Linux» srcset=»https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/locating-command-linux.png?w=795&ssl=1 795w, https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/locating-command-linux.png?resize=300%2C103&ssl=1 300w, https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/locating-command-linux.png?resize=768%2C263&ssl=1 768w, https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/locating-command-linux.png?resize=100%2C34&ssl=1 100w, https://i0.wp.com/itsfoss.com/wp-content/uploads/2021/01/locating-command-linux.png?resize=150%2C51&ssl=1 150w» sizes=»(max-width: 795px) 100vw, 795px» data-recalc-dims=»1″/>

When you specify the script WITHOUT the absolute or relative path, it cannot find it in the directories mentioned in the PATH variable.

Why most shell scripts contain #! /bin/bash at the beginning of the shell scripts?

Remember how I mentioned that shell is just a program and there are different implementations of shells.

When you use the #! /bin/bash, you are specifying that the script is to run with bash as interpreter. If you don’t do that and run a script in ./script.sh manner, it is usually run with whatever shell you are running.

Does it matter? It could. See, most of the shell syntax is common in all kind of shell but some might differ.

For example, the array behavior is different in bash and zsh shells. In zsh, the array index starts at 1 instead of 0.

» data-medium-file=»https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/01/bash-vs-zsh.png?fit=300%2C146&ssl=1″ data-large-file=»https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/01/bash-vs-zsh.png?fit=795%2C386&ssl=1″ loading=»lazy» width=»795″ height=»386″ src=»https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/01/bash-vs-zsh.png?resize=795%2C386&ssl=1″ alt=»Bash Vs Zsh» srcset=»https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/01/bash-vs-zsh.png?w=795&ssl=1 795w, https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/01/bash-vs-zsh.png?resize=300%2C146&ssl=1 300w, https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/01/bash-vs-zsh.png?resize=768%2C373&ssl=1 768w, https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/01/bash-vs-zsh.png?resize=100%2C49&ssl=1 100w, https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/01/bash-vs-zsh.png?resize=150%2C73&ssl=1 150w» sizes=»(max-width: 795px) 100vw, 795px» data-recalc-dims=»1″/> Bash Vs Zsh

Using #! /bin/bash indicates that the script is bash shell script and should be run with bash as interpreter irrespective of the shell which is being used on the system. If you are using zsh specific syntax, you can indicate that it is zsh script by adding #! /bin/zsh as the first line of the script.

The space between #! /bin/bash doesn’t matter. You can also use #!/bin/bash.

Was it helpful?

I hope this article added to your Linux knowledge. If you still have questions or suggestions, please leave a comment.

Expert users can still nitpick this article about things I missed out. But the problem with such beginner topics is that it is not easy to find the right balance of information and avoid having too much or too few details.

If you are interested in learning bash script, we have an entire Bash Beginner Series on our sysadmin focused website Linux Handbook. If you want, you may also purchase the ebook with additional exercises to support Linux Handbook.

Like what you read? Please share it with others.

Источник

Читайте также:  Порт вот под линукс
Оцените статью