User Tools

Site Tools


bash:commands:command_line_arguments

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
bash:commands:command_line_arguments [2021/01/26 16:57] – created peterbash:commands:command_line_arguments [2021/01/26 17:03] (current) peter
Line 3: Line 3:
 Getting arguments directly from the command shell can be beneficial in a number of cases. Getting arguments directly from the command shell can be beneficial in a number of cases.
  
-<code bash>+<file bash test.sh>
 #!/bin/bash #!/bin/bash
 echo "Total arguments : $#" echo "Total arguments : $#"
 echo "First Argument = $1" echo "First Argument = $1"
 echo "Second Argument = $2" echo "Second Argument = $2"
-</code>+</file>
  
 Run this script with two additional parameters after its name. Run this script with two additional parameters after its name.
- 
-I’ve named it test.sh and the calling procedure is outlined below. 
  
 <code bash> <code bash>
Line 18: Line 16:
 </code> </code>
  
-$1 is used for accessing the first argument, $2 for the second, and so on.+<WRAP info> 
 +**NOTE:**  **$1** is used for accessing the first argument, **$2** for the second, and so on.
  
-The $# is used for getting the total number of arguments.+The **$#** is used for getting the total number of arguments. 
 + 
 +</WRAP>
  
 ---- ----
Line 28: Line 29:
 The below example shows how to get command-line arguments with their names. The below example shows how to get command-line arguments with their names.
  
-<code bash>+<file bash test.sh>
 #!/bin/bash #!/bin/bash
  
Line 47: Line 48:
  
 echo "X+Y=$result" echo "X+Y=$result"
-</code>+</file>
  
-Name this script test.sh and call it as shown below.+Run this script with two additional parameters after its name.
  
 <code bash> <code bash>
Line 55: Line 56:
 </code> </code>
  
-It should return X+Y=144.+returns 
 + 
 +<code bash> 
 +X+Y=144 
 +</code> 
 + 
 +<WRAP info> 
 +**NOTE:**  The arguments here are stored inside **‘$@‘** and the script fetches them using the Linux **cut** command. 
 +</WRAP>
  
-The arguments here are stored inside ‘$@‘ and the script fetches them using the Linux cut command. 
  
bash/commands/command_line_arguments.1611680227.txt.gz · Last modified: 2021/01/26 16:57 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki