LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 11-13-2004, 02:15 PM   #1
Kropotkin
Member
 
Registered: Oct 2004
Location: /usr/home
Distribution: Mint, Ubuntu server, FreeBSD, Android
Posts: 362

Rep: Reputation: 32
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.
Old 11-13-2004, 02:20 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,367

Rep: Reputation: 562Reputation: 562Reputation: 562Reputation: 562Reputation: 562Reputation: 562
"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.
Old 11-13-2004, 03:32 PM   #3
amfoster
Member
 
Registered: Aug 2004
Distribution: debian, SuSE
Posts: 365

Rep: Reputation: 36
$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.
Old 11-17-2009, 04:23 PM   #4
kvmreddy
LQ Newbie
 
Registered: Aug 2009
Posts: 15

Rep: Reputation: 3
Thumbs up 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
 
Old 11-17-2009, 06:08 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,415

Rep: Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 07-25-2011, 07:14 AM   #6
xombboxer
Member
 
Registered: Apr 2011
Posts: 63

Rep: Reputation: 0
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
 
Old 07-25-2011, 07:21 AM   #7
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by xombboxer View Post
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
 
Old 07-25-2011, 07:21 AM   #8
Kropotkin
Member
 
Registered: Oct 2004
Location: /usr/home
Distribution: Mint, Ubuntu server, FreeBSD, Android
Posts: 362

Original Poster
Rep: Reputation: 32
Yes, read the messages above which explain how to do this.
 
Old 07-25-2011, 07:53 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
That's great -- a 7 year old thread gets a new post and OP Kropotkin replies the same minute!
 
Old 07-25-2011, 07:55 AM   #10
xombboxer
Member
 
Registered: Apr 2011
Posts: 63

Rep: Reputation: 0
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
 
Old 07-25-2011, 08:00 AM   #11
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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.
 
Old 07-25-2011, 08:12 AM   #12
xombboxer
Member
 
Registered: Apr 2011
Posts: 63

Rep: Reputation: 0
@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
 
Old 07-25-2011, 10:24 AM   #13
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
my database export shell script run from command line but not form crontab saifee Linux - General 11 01-29-2012 10:24 AM
How to execute a command line in a shell script orgazmo Programming 5 05-09-2005 10:16 AM
Shell Script to read 500files from the command line saravanan1979 Programming 1 09-22-2004 10:44 AM
Perl shell-out to script dunna work. Works on command line. Why? jlangelier Linux - Software 1 08-28-2004 03:00 AM
How to change parameter values of a function in shell script? Bassam Programming 0 01-25-2004 10:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration