Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get parameters by Linux shell

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to get parameters from Linux shell". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to get parameters from Linux shell"!

Method of passing parameters

How does Linux Shell get parameters $# is the number of parameters passed to the script $0 is the name of the script itself is the first parameter passed to the script 2 is the second parameter passed to the shell script is a list of all parameters passed to the script * is a single string to display all parameters passed to the script, unlike position variables, parameters can exceed 9$ is the current process of the script running ID number $? Displays the exit status of the last command. 0 means there is no error, and others indicate that there is an error.

Give an example

Create a new test.sh

#! / bin/shecho "shell script name: $0" echo "the first parameter passed to shell: $1" echo "to shell the second parameter: $2"

Execute on the command line

Chmod + x test.sh./test.sh 4 5

And *

Identical point

All parameters are referenced.

Differences

And @ both indicate that all arguments passed to a function or script are "2" when they are not contained in double quotes (""). Outputs all parameters in the form "$n".

When they are enclosed in double quotes (""), "output all parameters as a whole in the form of 1 n"; "separate the parameters and output all parameters in the form of 1"n". Create a new file.sh and write the following:

#! / bin/bashecho "-" for key in "$@" doecho'$@'$keydoneecho "- -" for key2 in $* doecho'$*'$key2done

1. Execution and result in quotation marks:

[root@localhost ~] # bash file.sh linux "python c"

2. Execution without quotation marks and result:

[root@localhost ~] # bash file.sh linux python c so far, I believe you have a deeper understanding of "how to get parameters from Linux shell". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report