Skip to content

[Linux] Input Command Line Arguments to Shell Script

In Linux, we can call the computer to complete the functions we want to complete, whether it is automatic sorting of letters, automatic file sorting, performance testing, repeated execution of a certain task.

We call all do it by creating shell script in Linux.

And a convenient and easy-to-use shell script can input arguments from the command line. As I had such needs more and more times, I have an idea of recording the input arguments.


How to Input Arguments

Input arguments in shell script is quite simple. Basically, the symbols used to input arguments are as follows:

  • $#: Shows several externally input parameters
  • $0: File name
  • $1: The first argument
  • $2: The second argument
  • $n: The nth argument

That’ right, it’s so easy.

For example, I edit a test.sh file:

And input 1 and 2 into shell script

sh test.sh 1 2

Output:

In fact, it is very simple to use.


References


Read More

Tags:

Leave a Reply