HowTo Run a Script In Linux

How do I run a Linux shell script? How can I run a script in Linux?

By default script will not run. You need to set execute permission for your script. To execute or run script type the following command:
chmod +x script-name-here
OR
chmod 0755 script.sh
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
Run a script called backup.ksh using ksh shell:
$ ksh backup.ksh
Run a script called backup.bash using BASH shell:
$ bash backup.bash

Example

Create a shell script called hello.sh using a text editor:

 
#!/bin/bash
echo "Hello $USER."
echo "Today is $(date)"
echo "Current working directory : $(pwd)"

Save and close the file. Set the permission:
$ chmod +x hello.sh
$ ./hello.sh

If the current directory is in the PATH variable, you can avoid typing the ./ before the hello.sh. It is a good idea to create your own bin directory as follows:
$ mkdir $HOME/bin
Add $HOME/bin to the PATH variable using bash shell export command:
$ export PATH=$PATH:$HOME/bin
$ echo $PATH

Move hello.sh in $HOME/bin using the mv command:
$ mv hello.sh $HOME/bin
Execute the script:
$ hello.sh
Sample outputs:

Hello vivek.
Today is Thu Nov 10 17:49:15 IST 2011
Current working directory : /nafiler05/users/v/vivek/bin/demos

Republished by Blog Post Promoter

About author

admin
admin 87 posts

I have been working with Linux based systems since 2002. I hope the article published on this site will be useful to those new to server administration.

You might also like

Linux Basics 0 Comments

Install EPEL Repo on a CentOS and RHEL 7

How do I install the extra repositories such as Fedora EPEL repo on a Red Hat Enterprise Linux server version 7.x or CentOS Linux server version 7.x? You can easily

Cpanel/WHM 0 Comments

cPanel: Apache + PHP FastCGI Data Timeout Error

If you are run on cPanel server and have FastCGI enabled in Apache, you might facing following error which caused your website hang or prompting ’500 Internal Server Error’ message.

Linux Basics 0 Comments

DOS-to-UNIX file conversion

Using Perl: # WARNING! For UNIX only! Do not use with ActivePerl in MSYS shell on Windows! # (Returns error message and file is deleted.) perl -pi -e ‘s/\r\n/\n/g’ filename