Simple use of basename in the shell

Source: Internet
Author: User

In a shell script, the $ A is typically used to display the name of the script, which displays the path name of the script when not using basename


For example

Cat test5.sh #!/bin/bash # Testing the $ parameter echo the zero parameter is set to:$0

Execute script

bash/root/shell/test5.sh

The zero parameter is set to:/root/shell/test5.sh # #显示了脚本的路径


After adding basename

#!/bin/bash # Testing the $ parameter name=$ (basename $) echo the zero parameter is set to: $name


Execute script

bash/root/shell/test5b.sh

The zero parameter is set to:test5b.sh # #直接显示脚本名称


A simple instance that performs different functions according to the different names of the scripts, when the script name is Addem, the addition is performed, and the multiplication is multem.


The script is as follows

#!/bin/bash#testing a multi-function Script name=$ (basename $) #if [$name = "Addem"]then total=$[$ + $]elif [$nam E = "Multem"]then total=$[$]fiecho The calculated value is $total



CP test6.sh Addem

CP test6.sh Multem


Execute Addem Script


./addem 25 3

The calculated value is 28


Execute Multem Script


[[Email Protected]_71_179_centos shell]#./multem 3 5

The calculated value is 15


This article is from the "Sdsca" blog, make sure to keep this source http://sdsca.blog.51cto.com/10852974/1903822

Simple use of basename in the shell

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.