LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-06-2007, 04:07 PM   #1
antis
Member
 
Registered: Nov 2004
Location: sweden
Distribution: Arch
Posts: 67

Rep: Reputation: 15
shell script, Passing string as argument?


Hi,

I have a function in a ksh script to which I'm trying to pass a string as an argument. Like this:

Code:
function myFunct {
echo $1
}

VAR='this is a test string'

myFunct $VAR
The problem is that when I pass the variable $VAR to the function the argument is split into $1, $2, $3, $4, $5 which causes the "echo $1" to only print "this". This is not the case if I pass the string directly to the function, like this:
Code:
myFunct 'this is a test string'
This way "echo $1" prints the whole string.

How would I go about using the variable and get the function to treat it as one whole string? I've tried every qutoing way that I know of but can't get it to work.
 
Old 09-06-2007, 04:11 PM   #2
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
Doesn't this work? VAR="this is a test string"
 
Old 09-06-2007, 04:23 PM   #3
antis
Member
 
Registered: Nov 2004
Location: sweden
Distribution: Arch
Posts: 67

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by gnashley View Post
Doesn't this work? VAR="this is a test string"
No, still just results in "this"...
 
Old 09-06-2007, 04:58 PM   #4
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,497
Blog Entries: 2

Rep: Reputation: 68
Protect the argument when passing it to your function:
Code:
myFunct "$VAR"
This is true for every *nix command, not just for your own functions/programs, e.g.
Code:
rm "a filename with spaces on it"
 
Old 09-07-2007, 02:03 AM   #5
antis
Member
 
Registered: Nov 2004
Location: sweden
Distribution: Arch
Posts: 67

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by marozsas View Post
Protect the argument when passing it to your function:
Code:
myFunct "$VAR"
This is true for every *nix command, not just for your own functions/programs, e.g.
Code:
rm "a filename with spaces on it"
Oh my... that was simple.
Time to read up on the differences between hard and soft quotes I guess.
Thanks!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Bash script - passing command as string lenzj Programming 3 08-24-2006 12:36 PM
Passing parameters to a shell script neocookie Linux - General 5 10-20-2005 12:44 PM
Passing arguments to a shell script subu_s Programming 3 09-02-2005 06:13 AM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 05:32 AM
PHP Script argument passing error... lokee Linux - Software 5 04-24-2003 10:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:52 PM.

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