Shell programming special variable -- location variable

Source: Internet
Author: User
Shell special variables & #9758; location variables 1 and $0 get the file name of the current script, including the path. # Write a test script vimtest. sh # The content is as follows: dirname $0 basename $0 # execute bash $ (pwd)/test. sh # output:/home/janetest. sh2, $ n get the nth parameter of the script being executed, n = 1 .. 9, $0, current

Shell special Variables☞Location variable


1. $0 get the file name of the current script, including the path.

# Write a test script vim test. sh # The content is as follows: dirname $0 basename $0 # execute bash $ (pwd)/test. sh # output the following/home/janetest. sh

2. $ n: Obtain the nth parameter of the currently executed script, n = 1 .. 9, $0, which is the name of the current script. If n is greater than 9, use $ {10}

Echo 'echo '$ (seq-s "$" 1 5 | sed's/1/$1/')> test_n.shcattest_n.sh # Content: # echo $1 $2 $3 $4 $ 5bashtest_n.sh arg1 agr2 arg3 # output content: # arg1 agr2 arg3


3. $ * Get all Script Parameters

Echo 'echo $ * '> test _ *. shcattest _ *. sh # Content: # echo $ * bashtest _ *. sh 1 2 3 # output: #1 2 3


4. $ # obtain the number of all parameters of the script

Echo 'echo $ # '> test_j.shcattest_j.sh # Content: # echo $ # bashtest_j.sh 1 2 3 # output: #3



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.