LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to pass a string appended to script? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-pass-a-string-appended-to-script-4175583581/)

Hot Beef 07-02-2016 02:01 AM

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!

Jjanel 07-02-2016 02:31 AM

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]

Turbocapitalist 07-02-2016 03:26 AM

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.

jpollard 07-02-2016 07:38 AM

There are also environment variables...

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


All times are GMT -5. The time now is 08:24 PM.