Introduction to the position variables of special variables in shell programming _linux shell

Source: Internet
Author: User

1. Get the file name of the current script, including the path.

Copy Code code as follows:

#写一个测试脚本
Vim test.sh
#内容如下
Dirname$0
Basename$0
#执行一下
bash$ (PWD)/test.sh
#输出如下
/home/jane
test.sh

2. $n get the nth parameter of the current execution script, n=1..9,$0, for the current script name. If n is greater than 9, use ${10}

Copy Code code as follows:

Echo ' Echo ' $ (seq-s "$" 1 5|sed ' s/1/$1/') > test_n.sh
cattest_n.sh
#内容如下
#echo $ $ $ $
bashtest_n.sh arg1 AGR2 Arg3
#输出内容:
#arg1 AGR2 Arg3

3, $* get all the parameters of the script

Copy Code code as follows:

Echo ' echo $* ' >test_*.sh
cattest_*.sh
#内容如下:
#echo $*
Bashtest_*.sh 1 2 3
#输出:
#1 2 3

4, $# get the number of all the parameters of the script

Copy Code code as follows:

Echo ' echo $# ' >test_j.sh
cattest_j.sh
#内容如下:
#echo $#
Bashtest_j.sh 1 2 3
#输出:
#3

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.