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: Fedora, Mint, FreeBSD, Android
Posts: 348

Rep: Reputation: 31
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
Guru
 
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Debian Wheezy, Debian Jessie
Posts: 7,593

Rep: Reputation: 188Reputation: 188
"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: 34
$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
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
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: 56

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,614
Blog Entries: 1

Rep: Reputation: 1052Reputation: 1052Reputation: 1052Reputation: 1052Reputation: 1052Reputation: 1052Reputation: 1052Reputation: 1052
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: Fedora, Mint, FreeBSD, Android
Posts: 348

Original Poster
Rep: Reputation: 31
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: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
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: 56

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,614
Blog Entries: 1

Rep: Reputation: 1052Reputation: 1052Reputation: 1052Reputation: 1052Reputation: 1052Reputation: 1052Reputation: 1052Reputation: 1052
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: 56

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,614
Blog Entries: 1

Rep: Reputation: 1052Reputation: 1052Reputation: 1052Reputation: 1052Reputation: 1052Reputation: 1052Reputation: 1052Reputation: 1052
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


All times are GMT -5. The time now is 02:26 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration