0% found this document useful (0 votes)
41 views1 page

Techskills Linuxshellscriptingbasics 6 1 1 Userinput Positional Parameters

This document discusses positional parameters in Linux shell scripts. It defines positional parameters as command line arguments passed to a script, which are assigned to special variables $0-$9. If more than 9 parameters are passed, they must be enclosed in curly braces. The $# variable tracks the number of parameters passed, and $* and $@ treat the parameters as a single string or array, respectively. The shift command moves all parameters to the left by default, useful for iterating through parameters of unknown number. Examples are provided to demonstrate incorporating positional parameters and the shift command in shell scripts.

Uploaded by

Angelo Gamo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views1 page

Techskills Linuxshellscriptingbasics 6 1 1 Userinput Positional Parameters

This document discusses positional parameters in Linux shell scripts. It defines positional parameters as command line arguments passed to a script, which are assigned to special variables $0-$9. If more than 9 parameters are passed, they must be enclosed in curly braces. The $# variable tracks the number of parameters passed, and $* and $@ treat the parameters as a single string or array, respectively. The shift command moves all parameters to the left by default, useful for iterating through parameters of unknown number. Examples are provided to demonstrate incorporating positional parameters and the shift command in shell scripts.

Uploaded by

Angelo Gamo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Filename:techskills-linuxshellscriptingbasics-6-1-1-userinput-positional-parameters.

md
Show Name: Linux Shell Scripting Basics**
Topic Name: User Input
Episode Name: User Input: Positional Parameters
Description: In this episode, Daniel and Justin go over working
with postional parameters in conjunction with bash scripts. Here
they define positional and their special variables; showing their
incorporation into your scripts to pass information from the user.
They also look at the shift command for moving parameters when
necessary.
Keywords: [keyword1,keyword2,keyword3]

User Input: Positional Parameters


Passing Parameters
Command Line Parameters
EXAMPLE: cli_params.sh 10 20 Don "Bill Paxton"
Everything, including the script name are assigned Positional Parameters
They are special variables
./cli_parms.sh 10 20 Don "Bill Paxton"
------$0-----------$1-$2--$3------$4-------
EXAMPLE parameters.txt
Positional parameters are from $0 - $9
If you need more than 9 then they must be surrounded with {}
${10} ${11} ${12} etc
EXAMPLE: cli_params_high.sh
$# keeps track of the number of command line params passed
Works like any other variable
Can be used to make sure all required params are set
EXAMPLE: ./cli_params_count.sh
Modify to !# in conjunction with ${} to grab the last parameter
passed
MODIFY: ./cli_params_count.sh
add: "echo ${!#} was the last parameter passed."
$* treats all arguments passed as a single string
EXAMPLE: ./cli_params_string.sh
Make sure to surround special variables with DOUBLE QUOTES
$@ treats all arguments passed as an array
Typically used in for loops
EXAMPLE: ./cli_params_array.sh
Don't forget those DOUBLE QUOTES!!!
shift moves the positional parameters to the left
Moves 1 position to the left by default
$4 becomes $3, $3 becomes $2, and so on
$0 is unaffected
You can specify more spaces to shift
shift 3 moves three positions to the left
Useful for iterating through parameters when the number of parameters
passed is unknown
Reduces code by allowing you to operate on the first parameter,
shift, then operate on the first parameter again
May remove the need to use ${10} type parameters
EXAMPLE: cli_params_shift.sh
Very basic. Best if you use names and not numbers
EXAMPLE: cli_params_shift2.sh
More descriptive example

You might also like