LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 07-02-2016, 02:01 AM   #1
Hot Beef
LQ Newbie
 
Registered: Apr 2016
Distribution: Debian 6
Posts: 3

Rep: Reputation: Disabled
How to pass a string appended to script?


I want to have a few variables already created in the script, but then be able to append these variable names to the script being run; to run customized commands using a case statement.

Like this:
./script customA
./script customB

I have found many tutorials on prompting the user for input, but I need to pass a parameter to my script before it starts, thus running a predefined command inside of the case statement.
Thanks!

Last edited by Hot Beef; 07-02-2016 at 02:03 AM.
 
Old 07-02-2016, 02:31 AM   #2
Jjanel
Member
 
Registered: Jun 2016
Distribution: any&all, in VBox; Ol'UnixCLI; NO GUI resources
Posts: 999
Blog Entries: 12

Rep: Reputation: 364Reputation: 364Reputation: 364Reputation: 364
Hi! Did you mean like $1, which google shows via a search for:
shell script arguments
(or something more obscure/complex)? [quoting issues IF var not 1word]

Last edited by Jjanel; 07-02-2016 at 03:34 AM. Reason: my 1st use of "pretty" msg formatting! Cool ;-)
 
Old 07-02-2016, 03:26 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Yes, $1 is one way of writing a reference to the first parameter used with the script.

See the special variables $# and ${1}, ${2}, and so on. The ${#} tells you the number of arguments used to launch the script ${1} tells you the first, ${2} tells you the second and so on. If you use braces { } around the variable names, you can go into double-digits like with ${10} and ${11} if you need. The built-in command "shift" will work on $#, too.

The manual page for the shell you are using (sh, dash, bash) will have some more details.
 
Old 07-02-2016, 07:38 AM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
There are also environment variables...

and "include" scripts to set local variables...
 
  


Reply



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
How to pass string variable as command parameter in shell script? prasad1990 Linux - Newbie 6 03-23-2015 12:48 AM
[SOLVED] Pass space (or string escaped) as argument to shell script. scmbg Programming 1 10-07-2009 10:15 AM
bash script path issue - how to pass a path as a string to a variable PiNPOiNT Programming 5 04-17-2009 05:48 PM
fdisk -l output with partition label appended - Bash script available Emmanuel_uk Programming 7 06-10-2006 05:08 AM
Wierd String appended to end of line lel800 General 8 12-28-2004 03:55 PM

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

All times are GMT -5. The time now is 07:24 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