I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…
The Unix shell is used to run commands, and it allows users to pass run-time arguments to these commands.
These arguments, also known as command line parameters, allow the users to either control the flow of the command or to specify the input data for the command.
Unix Video #22:
In this tutorial, we will understand how to work with command line parameters.
While running a command, the user can pass a variable number of parameters in the command line.
Within the command script, the passed parameters are accessible using ‘positional parameters’. These range from $0 to $9, where $0 refers to the name of the command itself, and $1 to $9 are the first through to the ninth parameter, depending on how many parameters were passed.
Example:
$ sh hello how to do you do
Here $0 would be assigned sh
$1 would be assigned hello
$2 would be assigned how
And so on …
We will now look at some additional commands to process these parameters.
#1) set
This command can be used to set the values of the positional parameters on the command line.
Example:
$ set how do you do
$ echo $1 $2
how do
Here, “how” was assigned to $1 and “do” was assigned to $2 and so on.
#2) shift
This command is used to shift the position of the positional parameters. i.e. $2 will be shifted to $1 all the way to the tenth parameter being shifted to $9. Note that if in case there are more than 9 parameters, this mechanism can be used to read beyond the 9th.
Example:
$ set hello good morning how do you do welcome to Unix tutorial.
Here, ‘hello’ is assigned to $1, ‘good’ to $2 and so on to ‘to’ being assigned to $9. Now the shift command can be used to shift the parameters ‘N’ places.
Example:
$ shift 2
$ echo $1
Now $1 will be ‘morning’ and so on to $8 being ‘unix’ and $9 being ‘tutorial’.
Check our upcoming tutorial to know more about Processes in Unix.
learn Cut Command in Unix with Simple and Practical Examples: Unix provides a number of filter commands that can be used for processing flat file databases. These filter commands can be chained together to perform a series of operations with a single command. A flat file database is a file…
Learn Unix Cat Command with Examples: The cat command is perhaps the most commonly used Unix command. It is derived from catenate – which describes the process of connecting things. The cat command is a filter that can be used for multiple purposes: Display the contents of text files. Copy text…
Learn Tar Command in Unix with practical Examples: The primary function of the Unix tar command is to create backups. It is used to create a ‘tape archive’ of a directory tree, that could be backed up and restored from a tape-based storage device. The term ‘tar’ also refers to the…
Learn ls Command in Unix with examples: The Ls command is used to get a list of files and directories. Options can be used to get additional information about the files. Know ls command syntax and options with practical examples and output. ls Command in Unix with Examples ls Syntax: ls…
A Brief Introduction To Command Line Arguments In C++. We have already seen the usage of arguments or parameters in our tutorial on functions. We also learned the purpose of passing arguments to/from functions. We can also have arguments passed to the main function. These are in turn known as…
Learn Grep Command in Unix with Practical Examples: Grep command in Unix/Linux is the short form of ‘global search for the regular expression’. The grep command is a filter that is used to search for lines matching a specified pattern and print the matching lines to standard output. Grep Command…
In this tutorial, you will learn different basic and advanced Unix Commands. Unix commands are inbuilt programs that can be invoked in multiple ways. Here, we will work with these commands interactively from a Unix terminal. A Unix terminal is a graphical program that provides a command-line interface using a…
Overview of Unix Shell Functions: Shell Functions are used to specify the blocks of commands that may be repeatedly invoked at different stages of execution. The main advantages of using Unix Shell Functions are to reuse the code and to test the code in a modular way. This tutorial will…