Passing Arguments to the Bash Script

 Passing Arguments to the Bash Script 


Passing Arguments to the Bash Script


Today we will be looking at how to parse the arguments to the bash script 

If you want to know more stuff about bash scripting you can visit Bash Scripting Tutorial

List of Contents 

  • Example of argument passing 

  • Parsing the Arguments 

Example of argument passing 

Suppose consider we are 'updating' our system, we use the command apt update then you can see the apt as the file & update as the argument we have passed to the file or command. 

Parsing the Arguments 

The arguments are stored in a variable from $1 - $9 

  • bash bash_script.sh LetsConfigure ( the LetsConfigure is the argument passed to the bash Script  )
Passing Arguments to the Bash Script

As you can see we have passed an argument to the bash script, which the script's output below that you can see the code of the bash script, as you can see we have just used $1 as you can consider the $1 represents the first argument passed to the script. 

You can also give other arguments also with the consecutive numbers 

Passing Arguments to the Bash Script

There are some special variables in the Bash Scripting 

  • $$ - Give the Process ID

  • $0 - Gives the file name 

  • $# - Gives the number of arguments passed to the script 

  • $* - Give all the arguments passed 

  • $@ - Gives the value to an array
Passing Arguments to the Bash Script

Note:- 

If you put the arguments in the quotes, then it is considered a single argument.

Passing Arguments to the Bash Script

There are some other arguments in the Bash Scripting you can give, we will cover that in our later tutorials as we get to know the bash script more & more. 

Conclusion:- 

We have learned to pass the arguments to the bash script & different types of variables in the bash scripting. 


Stay Tuned ✌✌


Next Post Previous Post
No Comment
Add Comment
comment url