Solved

Linux Shell Scripting: Passing String Values

Posted on 2013-11-08
3
344 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

688 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