Passing command line parameters to script
So far, we have seen the usage of the commands such as grep
, head
, ls
, cat
, and many more. These commands also support passing arguments to a command via a command line. Some of command line arguments are input files, output files, and options. Arguments are provided as per output needs. For example, ls -l filename
is executed to get a long listing output, while ls -R filename
is used to display recursively the contents of a directory.
Shell script also supports providing command line arguments that we can process further by a shell script.
The command line arguments can be given as follows:
<script_file> arg1 arg2 arg3 … argN
Here, script_file
is a shell script file to be executed, and arg1
, arg2
, arg3
, argN
, and so on, are command line parameters.
Reading arguments in scripts
Command line arguments are passed to a shell script as positional parameters. So, arg1
will be accessed in a script as $1
, arg2
as $2
, and so on.
The following shell demonstrates...