Published on March 4, 2017 by ProgrammingKnowledge
Want create site? Find Free WordPress Themes and plugins.

Passing arguments to a shell script

Any shell script you run has access to (inherits) the environment variables accessible to its parent shell. In addition, any arguments you type after the script name on the shell command line are passed to the script as a series of variables.

The following parameters are recognized:

$*
Returns a single string (“$1, $2 … $n”) comprising all of the positional parameters separated by the internal field separator character (defined by the IFS environment variable).

$@
Returns a sequence of strings (“$1”, “$2”, … “$n”) wherein each positional parameter remains separate from the others.

$1, $2 … $n
Refers to a numbered argument to the script, where n is the position of the argument on the command line. In the Korn shell you can refer directly to arguments where n is greater than 9 using braces. For example, to refer to the 57th positional parameter, use the notation ${57}. In the other shells, to refer to parameters with numbers greater than 9, use the shift command; this shifts the parameter list to the left. $1 is lost, while $2 becomes $1, $3 becomes $2, and so on. The inaccessible tenth parameter becomes $9 and can then be referred to.

$0
Refers to the name of the script itself.

$#
Refers to the number of arguments specified on a command line.

Did you find apk for android? You can find new Free Android Games and apps.

Leave a Reply

5 Comments on "Shell Scripting Tutorial for Beginners 4 – Pass Arguments to a Bash-Script"

Notify of
avatar

SUJIT KUMAR NAYAK
Guest
SUJIT KUMAR NAYAK
3 months 24 days ago

#!bin/bashread -p "Enter user name: " user_varread -sp "Enter password: " pass_varecho " The user name is : $user_var"echo " Password is : $pass_var"write the script as u told but it showing errorsujits@sujits-ubuntu:~$ sh hello.sh Enter user name: sonu hello.sh: 3: read: Illegal option -s The user name is : sonu Password is :

Nilesh D
Guest
Nilesh D
4 months 13 days ago

very good,really helpful for me

tushar ghude
Guest
tushar ghude
7 months 3 hours ago

good tutorials one request – please explain how array (or similar tool/program functionality) works in details before using them in examples tip for beginners: if ur scratching ur head wondering why shell didnt print actual values but only variable names when he used echo 'echo > $0 $1 $2 $3'its because shell doesnt expand variables in single quotes (but it does in double quotes – see prev examples)

Sirous Mani
Guest
Sirous Mani
8 months 17 days ago

Make some apache videos later please too. Generically.

Jose Gonzalez
Guest
Jose Gonzalez
8 months 17 days ago

Cual es la diferencia entre ftp y sambaSaludos desde centro América

wpDiscuz