LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-28-2014, 06:04 AM   #1
theEditor876
LQ Newbie
 
Registered: Jun 2014
Posts: 4

Rep: Reputation: Disabled
Passing arguments with SPACES to bash script


Hey guys!

Lets say i have a shell script looking like this:

Code:
#!/bin/bash
var1="wehavea spacehere"
var2="11.02.2013"

./dosomething.sh $var1 $var2
dosomething.sh:

Code:
#!/bin/bash

whatever=123

./someprogram $1 $2 $whatever
when i pass $var1 and $var2 to dosomething.sh, $var1 will get split up in 2 arguments!
calling dosomething.sh like this:

Code:
./dosomething.sh "$var1" "$var2"

does not work, either.

Can somebody tell me how to properly pass arguments with a space in between them?
Thank you!
 
Old 06-28-2014, 07:30 AM   #2
mark_alfred
Senior Member
 
Registered: Jul 2003
Location: Toronto, Ontario, Canada
Distribution: Ubuntu Linux 16.04, Debian 10, LineageOS 14.1
Posts: 1,572

Rep: Reputation: 210Reputation: 210Reputation: 210
I'm not sure, but perhaps a backward slash would work.

Code:
#!/bin/bash
var1="wehavea\ spacehere"
var2="11.02.2013"

./dosomething.sh $var1 $var2
 
Old 06-28-2014, 07:41 AM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by theEditor876 View Post
dosomething.sh:

Code:
#!/bin/bash

whatever=123

./someprogram $1 $2 $whatever
when i pass $var1 and $var2 to dosomething.sh, $var1 will get split up in 2 arguments!
calling dosomething.sh like this:

Code:
./dosomething.sh "$var1" "$var2"

does not work, either.

Can somebody tell me how to properly pass arguments with a space in between them?
You are passing the argument correctly there, but dosomething.sh isn't using it correctly. You need to quote the arguments there, too:
Code:
./someprogram "$1" "$2" $whatever
 
  


Reply



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
[SOLVED] Passing arguments with blanks to du in a bash shell script impert Programming 13 06-12-2014 04:57 AM
Bash Script passing arguments dimitriauxio Linux - Newbie 8 09-16-2013 09:19 AM
[SOLVED] Bash script passing file names with spaces into another program yilez Programming 10 04-04-2013 05:45 AM
[SOLVED] BASH - passing arguments Garrett85 Programming 4 01-09-2013 03:03 PM
[SOLVED] passing commands as arguments to functions in bash script mastahyeti Linux - General 3 05-31-2010 06:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:46 AM.

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