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