Blog

03/06/2019

How to execute a shell script in Linux



I am a new Linux system user. How do I execute a shell script in Linux? How do I write and execute a shell script on Linux system?

This page shows various steps to write and execute a shell script in Linux using the Terminal window or app.

Steps to execute a shell script in Linux

The procedure is as follows:

  1. Create a new file called demo.sh using a text editor such as nano or vi in Linux: nano demo.sh
  2. Add the following code:
    #!/bin/bash
    echo "Hello World"
  3. Set the script executable permission by running chmod command in Linux: chmod +x demo.sh
  4. Execute a shell script in Linux: ./demo.sh

Let us see all steps in details.

Step 1 – Open the terminal

To write a shell script, open the terminal app. Hit the keyboard shortcut Ctrl-Alt+T:

Step 2 – Write the shell script using an editor

Type the following command to write a shell script:
nano demo.sh
OR
vi demo.sh

Creating your first shell script named demo.sh

Add the following code:

#!/bin/bash
# My first shell script
echo "Hello $USER"
echo "Today is $(date)"
echo "Bye for now"

#!/bin/bash # My first shell script echo "Hello $USER" echo "Today is $(date)" echo "Bye for now"

To Save and close the file in nano press CTRL+X and when prompted choose Y (Yes) to save the file and followed by the Enter key:

Writing your first shell script

Step 3 – Make the shell script executable

Let us verify newly created file, run [nicmd name=”ls”] and cat command:
ls -l demo.sh
cat demo.sh

To set executable permission, run the following chmod command:
chmod +x demo.sh
Verify permissions:
ls -l demo.sh

Give execute permission to your script on Linux

Step 4 – Execute the shell script in Linux

Now we have shell script named demo.sh. But, how do you run it? Try:
./demo.sh
The . refers to the current directory. Another option is to specify the full path:
/path/to/demo.sh
/home/vivek/demo.sh
~/demo.sh

Executing a shell script in Linux

Conclusion

For more info see Linux Shell Scripting Tutorial and bash command man page:
man bash
help read

(adsbygoogle = window.adsbygoogle || []).push({});

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....
12/08/2019

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...