Solved

Linux Shell Scripting: Passing String Values

Posted on 2013-11-08
3
340 Views
Last Modified: 2013-11-08
How can I pass the value of x and y through the command path?

For example:

sh ./xyz.sh Hello World
#!/bin/sh
$x="Hello";
$y="World";
echo $x $y;

Open in new window

0
Comment
Question by:hankknight
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
simon3270 earned 500 total points
ID: 39633552
#!/bin/sh
$x="$1"
$y="$2"
echo $x $y

Open in new window

call it as, for example
   myscript.sh Hello World
0
 
LVL 19

Expert Comment

by:simon3270
ID: 39633561
Note that you can only have 9 parameters passed like this ($1 to $9).  If you try to refer to $10 in your script, it will use the value of $1 and append a zero to that value.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 39635101
To refer to parameters 10-255, you need to enclose them in braces, eg:

${10}
${11}
..
${255}

The only shell that doesn't support this is the tradition bourne shell.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question