Linux / UNIX: Command For Executing a Shell Script

How do I execute a script under UNIX or Linux like operating system using command prompt?

First, you need to set the execute permissions on your script. This can be done using the chmod command as follows:
$ chmod +x your-script-name-here
$ chmod u+x your-script-name-here
Next, check that you got the permissions that you want with the ls -l command:
$ ls -l your-script-name-here
Finally, you can execute a script using any one of the following syntax:
$ ./your-script-name-here
$ bash your-script-name-here
$ sh your-script-name-here
$ ksh /nas/server/scripts/my.ksh

Bash Shell Script Example

Create a shell script called
$ vi
Add the following code:

# - A sample shell script
# Author - Vivek Gite
# Tested under Debian GNU/Linux using Bash shell version 4.x
# -----------------------------------------------------------------------------
echo "Hello $USER,"
echo "Current date and time set to \"$(date)\" on the \"$HOSTNAME\" system."
echo "Amount of free and used memory in the system:"
free -m

Save and close the file. Run it as follows:
$ chmod u+x
$ ./

Sample outputs:

Hello vivek,

Current date and time set to "Fri Jul 15 15:24:12 IST 2011" on the "" system.

Amount of free and used memory in the system:

             total       used       free     shared    buffers     cached
Mem:          7930       3699       4230          0         78       2115
-/+ buffers/cache:       1505       6424
Swap:         8114  

More links and information :

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
1 comment… add one
  • Shuaib Shahzan Jun 27, 2013 @ 11:05

    We could also execute the script without setting the execute permissions on the script.

    ex: .

    Even if the execute permissions are not set for the script, even then it will get executed.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum