Solved

Linux Shell Scripting: Passing String Values

Posted on 2013-11-08
3
337 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
Comment Utility
#!/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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now