1. Home
  2. Bash
  3. Bash Tutorial
  4. Bash – Command Arguments

Bash – Command Arguments

Command Line Arguments in Shell Script

Command line arguments are also known as positional parameters. These arguments are specific with the shell script on terminal during the run time. Each variable passed to a shell script at command line are stored in corresponding shell variables including the shell script name.

Synatx:

./myscript.sh ARG1 ARG2 ARG3 ARG4 ARG5 ARG6 ARG7 ARG8 ARG9 ARG10

See the below image to understand the command line values and variables. Here ARG1, ARG2 to ARG10 are command line values, which is assigned to corresponding shell variables.

What is command line arguments in shell scripts

These are also known as special variables provided by the shell. Except above screenshot, there are some more special variables as given below.

 Special Variable
 Variable Details
 $1 to $n
$1 is the first arguments, $2 is second argument till $n n’th arguments. From 10’th argument, you must need to inclose them in braces like ${10}, ${11} and so on
 $0
The name of script itself
 $$
Process id of current shell
 $*
Values of all the arguments. All agruments are double quoted
 $#
Total number of arguments passed to script
 $@
Values of all the arguments
 $?
Exit status id of last command
 $!
Process id of last command

Example Script

Command line arguments can be passed just after script file name with space separated. If any argument have space, put them under single or double quote. Read below simple script.

Now execute this script with 2 arguments and found following results.

$ ./arguments.sh Hello TecAdmin

Tags , , , ,