LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 03-09-2005, 02:26 PM   #1
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Rep: Reputation: 30
Spaces in bash parameters


*bangs head on monitor*

There must be some way to do this, but I can't seem to find it. For purely educational reasons, I am trying to figure out how to pass multiple-word parameters, i.e. "This is me", to a bash script, and have it evaluate correctly.
Inside the script, echoing $1 prints out "This is me" correctly, but the variable is not valid in if statements.

$ cat ./testscript.sh
#!/bin/bash

echo $1
if [ $1 = "This is me" ]; then
echo "Yay"
else
echo "No..."
fi
$ ./testscript.sh "This is me"
This is me
./testscript.sh: line 4: [: too many arguments
No...

Escaping the spaces (./testscript.sh "This\ is\ me") doesn't seem to work, and I'm not sure what else to try

Thanks!
 
Old 03-09-2005, 02:37 PM   #2
drisay
Member
 
Registered: Sep 2004
Distribution: Slackware 10
Posts: 167

Rep: Reputation: 30
just add double quotes around your $1 in the if statement

Code:
#!/bin/bash

echo $1
if [ "$1" = "This is me" ]; then
echo "Yay"
else
echo "No..."
fi
 
Old 03-09-2005, 02:38 PM   #3
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Try putting quotes around $1 in the if line. Like so:

Code:
#!/bin/bash

echo $1
if [ "$1" = "This is me" ]; then
echo "Yay"
else
echo "No..."
fi
Edit: Beaten to it...

Last edited by deiussum; 03-09-2005 at 02:39 PM.
 
Old 03-10-2005, 09:29 AM   #4
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Original Poster
Rep: Reputation: 30
Oh, how very cool I wasn't aware that one *could* put quotes around a variable!

Thanks very much, both of you!!
 
Old 03-11-2005, 01:21 AM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,311

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
Very useful trick when dealing with filenames with spaces in them ... usually when dealing with Windows partitions/users...
Be aware that a lot of Unix cmds don't behave well with filenames with spaces in them, so try to avoid/convert as needed...
 
Old 03-11-2005, 07:57 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,298

Rep: Reputation: 175Reputation: 175
when using 'test' it's advisable to always
quote, otherwise it will often bomb out with spaces or
null variables
e.g:
Code:
[ -f "$filename" ] && echo OK || echo doh
 
  


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 with spaces Quantum0726 Programming 2 11-14-2005 10:26 PM
Bash Scripting Directories with Spaces gtwilliams Linux - Software 3 07-06-2005 04:16 PM
Spaces in filenames with BASH edenning Programming 12 01-27-2005 08:10 AM
bash and filenames with spaces doctorwebbox Linux - General 2 12-15-2004 09:55 PM
bash script ? -- spaces in passed parameters azwr Linux - Newbie 3 06-18-2004 07:57 PM


All times are GMT -5. The time now is 12:39 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration