Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
11-13-2004, 02:15 PM
|
#1
|
Member
Registered: Oct 2004
Location: /usr/home
Distribution: Mint, Ubuntu server, FreeBSD, Android
Posts: 362
Rep:
|
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.
|
|
|
Click here to see the post LQ members have rated as the most helpful post in this thread.
|
11-13-2004, 02:20 PM
|
#2
|
LQ Guru
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,367
|
"How does one pass a command-line parameter to a shell script?"
The command line parameters are referenced as $1, $2, etc.
--------------------------------
Steve Stites
|
|
2 members found this post helpful.
|
11-13-2004, 03:32 PM
|
#3
|
Member
Registered: Aug 2004
Distribution: debian, SuSE
Posts: 365
Rep:
|
$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.
|
|
2 members found this post helpful.
|
11-17-2009, 06:08 PM
|
#5
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,415
|
|
|
|
07-25-2011, 07:14 AM
|
#6
|
Member
Registered: Apr 2011
Posts: 63
Rep:
|
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
|
|
|
07-25-2011, 07:21 AM
|
#7
|
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
|
Quote:
Originally Posted by xombboxer
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
|
|
|
07-25-2011, 07:21 AM
|
#8
|
Member
Registered: Oct 2004
Location: /usr/home
Distribution: Mint, Ubuntu server, FreeBSD, Android
Posts: 362
Original Poster
Rep:
|
Yes, read the messages above which explain how to do this.
|
|
|
07-25-2011, 07:53 AM
|
#9
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
That's great -- a 7 year old thread gets a new post and OP Kropotkin replies the same minute!
|
|
|
07-25-2011, 07:55 AM
|
#10
|
Member
Registered: Apr 2011
Posts: 63
Rep:
|
i want it like this
shell.sh file
Code:
echo $PASS
echo $NAME
should give me
if i run
Quote:
./shell.sh PASS=xxx NAME=yyy
|
|
|
|
07-25-2011, 08:00 AM
|
#11
|
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
|
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.
|
|
|
07-25-2011, 08:12 AM
|
#12
|
Member
Registered: Apr 2011
Posts: 63
Rep:
|
@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
|
|
|
07-25-2011, 10:24 AM
|
#13
|
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
|
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 01:41 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|