Take action or execute a command based upon shell script name

last updated in Categories Gentoo Linux, Howto, Linux, Open source coding, RedHat/Fedora Linux, Shell scripting, Tips, Ubuntu Linux, UNIX

You would like to run different commands or task based upon a single shell script name. For example script name is backup Instead of writing 3 different script write one script and softlink using ln command:


=> backup – original script

=> backup.nas – Script to backup data to NAS device

=> backup.ftp – Script to backup data to FTP Server

=> backup.scp – Script to backup data to Remote SSH server

Use ln command to make link between backup and other files
$ ln -s backup backup.nas
$ ln -s backup backup.ftp
$ ln -s backup backup.scp

Now use basename command strip directory and suffix from script name. $0 variable always print scriptname.
echo My name is $(basename $0)


My name is backup

Let us store output to variable
ME=$(basename $0)

Now simply use the case statement to get script name. The case statement allows you to match several values against $ME variable. Here is final script:

ME=$(basename $0)
case $ME in
           echo "Write code to mount NAS using smbmount, use cp to copy data to NAS"
           echo "Write code to copy data using FTP"
           echo "Write code to copy data using OpenSSH scp command"
           echo "Syntax error - show some help"
           echo "Use $ME.nas or $ME.ftp or $ME.ftp"

To backup data to NAS just type

To backup data to FTP server just type


  • Ease of use
  • Saves time
  • Your work looks quite professional

See also:


Posted by: Vivek Gite

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.

2 comment

  1. Hi

    I am taking tar.gz backup on RH9 and copying the file to windows ftp server shceduled shell script. I am seeing the file size difference at both location. Windows server is with ntfs partition. What could be the reason , that i am seeing 4 to 5 mb increased file size on windows server that RH 9 linux server?

    Regards /mahesh

    Have a question? Post it on our forum!