LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to pass command-line parameter to shell script? (http://www.linuxquestions.org/questions/linux-newbie-8/how-to-pass-command-line-parameter-to-shell-script-254396/)

Kropotkin 11-13-2004 01:15 PM

how to pass command-line parameter to shell script?
 
Hello,

I am new to linux and very new to shell scripting.

How does one pass a command-line parameter to a shell script? In DOS/OS/2 terms, I am looking for the equivalent of %1 etc.

Thanks.

jailbait 11-13-2004 01:20 PM

"How does one pass a command-line parameter to a shell script?"

The command line parameters are referenced as $1, $2, etc.

--------------------------------
Steve Stites

amfoster 11-13-2004 02:32 PM

$0 is the name of the command
$1 first parameter
$2 second parameter
$3 third parameter etc. etc
$# total number of parameters
$@ all the parameters will be listed

but in all honesy, you can go to shelldorado.com and get everything you want to know on sh, ksh, csh, bash, javascript etc. Great site with tutorials, examples, everything you will need.

kvmreddy 11-17-2009 03:23 PM

Passing arguments to a shell script
 
Here is an article, How to pass arguments to a shell script

http://bashscript.blogspot.com/2009/...ll-script.html

I hope this will clear most of your doubts

chrism01 11-17-2009 05:08 PM

http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

xombboxer 07-25-2011 06:14 AM

Shell named parameter passing
 
Is there a way to pass named parameters to shell script
like

Code:

$] ./shell PASS=123 NAME='KKK'
shell.sh
Code:

echo $PASS
ECHO $NAME


sycamorex 07-25-2011 06:21 AM

Quote:

Originally Posted by xombboxer (Post 4424521)
Is there a way to pass named parameters to shell script
like

Code:

$] ./shell PASS=123 NAME='KKK'
shell.sh
Code:

echo $PASS
ECHO $NAME


Try:

Code:

PASS=123 NAME=KKK shell.sh

Kropotkin 07-25-2011 06:21 AM

Yes, read the messages above which explain how to do this.

catkin 07-25-2011 06:53 AM

That's great -- a 7 year old thread gets a new post and OP Kropotkin replies the same minute! :eek:

xombboxer 07-25-2011 06:55 AM

i want it like this

shell.sh file
Code:


echo $PASS
echo $NAME

should give me

Quote:

xxx
yyy
if i run

Quote:


./shell.sh PASS=xxx NAME=yyy

sycamorex 07-25-2011 07:00 AM

Sugar, I didn't notice that:)


xombboxer: I provided you with the solution.
By the way, in the future don't highjack other people's threads (especially 3-year old ones). Create your own thread instead.

xombboxer 07-25-2011 07:12 AM

@sycamorex

i was in dilemma whether to start a new thread or search for the similar one already exists.... so i came here and contunued... :(

ok


Back to my question..


you said
Quote:

PASS=123 NAME=KKK shell.sh
i really didn't understand anything.

can you explain me bit ( newbie :( )

how to pass the parameter and how to handle it inside the script file

thanks

sycamorex 07-25-2011 09:24 AM

Given that your shell.sh script looks like you posted above, you can execute it (ans pass the values to the variables) by typing what I suggested. The values will be passed to the script and echo will display the passed values. Is that what you wanted?


All times are GMT -5. The time now is 07:56 PM.