LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 05-10-2011, 12:33 AM   #1
atomicdog
LQ Newbie
 
Registered: May 2009
Location: San Diego
Distribution: Ubuntu
Posts: 4

Rep: Reputation: 0
bash variable as parameter to script


Hi,

I want to create a variable that when passed as a parameter to another bash script will keep its string quotes (so it stays as one parameter). What ways can I achieve this cleanly? I found one way of doing it but it doesn't look like an optimal choice to me.

Thanks, John

Code:
john@ubuntu:/usr/local/src$ cat foo.sh 
#!/bin/bash

echo $0
echo $1
echo $2
john@ubuntu:/usr/local/src$ cat build-foo-test.sh 
#!/bin/bash
TOP=pwd
GCC_IGNORE="'boehm-gc gnattools libada libffi libgfortran libgomp libjava libobjc libssp .svn'"

# This works...
./foo.sh ../gcc-4.5 "`echo ${GCC_IGNORE}`"

# I would like it to look like this though...
./foo.sh ../gcc-4.5 $GCC_IGNORE
Code:
john@ubuntu:/usr/local/src$ . build-foo-test.sh 
./foo.sh
../gcc-4.5
'boehm-gc gnattools libada libffi libgfortran libgomp libjava libobjc libssp .svn'
./foo.sh
../gcc-4.5
'boehm-gc
john@ubuntu:/usr/local/src$
Code:
john@ubuntu:/usr/local/src$ uname --all
Linux ubuntu 2.6.32-30-generic #59-Ubuntu SMP Tue Mar 1 21:30:46 UTC 2011 x86_64 GNU/Linux
john@ubuntu:/usr/local/src$ bash --version
GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
john@ubuntu:/usr/local/src$
 
Old 05-10-2011, 01:26 AM   #2
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Rep: Reputation: 12
maybe put the echo in the variable GCC_IGNORE ?
 
Old 05-14-2011, 01:40 PM   #3
atomicdog
LQ Newbie
 
Registered: May 2009
Location: San Diego
Distribution: Ubuntu
Posts: 4

Original Poster
Rep: Reputation: 0
Hi,

Thanks for the reply. I just tried your suggestion again but it still doesn't seem to work.
 
Old 05-14-2011, 04:24 PM   #4
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375

Rep: Reputation: 217Reputation: 217Reputation: 217
I'd suggest looking into escape characters maybe?

so using \' and again another \'...

as an example the following code shows how this can be used to get different outputs

Code:
#!/bin/bash
MYSTRING="hello, keep me quoted"
MYFIRSTFUNC="echo \"$MYSTRING\""
MYSECONDFUNC="echo $MYSTRING"
echo "value of first func: $MYFIRSTFUNC"
$MYFIRSTFUNC
echo "value of second func: $MYSECONDFUNC"
$MYSECONDFUNC
This will output as follows


value of first func: echo "hello, keep me quoted"
"hello, keep me quoted"
value of second func: echo hello, keep me quoted
hello, keep me quoted

as you can see, the first func keeps the value quoted.

Last edited by r3sistance; 05-14-2011 at 04:44 PM.
 
Old 05-14-2011, 08:50 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am not sure I understand the issue??
Code:
#!/bin/bash

m2f()
{
    echo "$1"
}

myf()
{
    echo "$1"
    m2f "$1"
}

x="'hello this is quoted'"

echo "$x"

myf "$x"
As long as x is passed quoted it retains the internal quotes. Would someone explain if we are talking about something else?
 
1 members found this post helpful.
Old 05-15-2011, 12:19 PM   #6
astromime
LQ Newbie
 
Registered: May 2011
Posts: 26

Rep: Reputation: 13
maybe try using an array?
 
Old 05-16-2011, 12:04 AM   #7
atomicdog
LQ Newbie
 
Registered: May 2009
Location: San Diego
Distribution: Ubuntu
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by r3sistance View Post
I'd suggest looking into escape characters maybe?
...
This will output as follows

value of first func: echo "hello, keep me quoted"
"hello, keep me quoted"
value of second func: echo hello, keep me quoted
hello, keep me quoted

as you can see, the first func keeps the value quoted.
It doesn't work as a parameter to another script for some reason.

Code:
john@ubuntu:/usr/local/src$ cat r3sistance 
#!/bin/bash

LIST="boehm-gc gnattools libada libffi libgfortran libgomp libjava libobjc libssp .svn"

GCC_IGNORE="echo \"$LIST\""

./foo.sh ../gcc-4.5 $GCC_IGNORE

$GCC_IGNORE

john@ubuntu:/usr/local/src$ . r3sistance 
./foo.sh
../gcc-4.5
echo
"boehm-gc gnattools libada libffi libgfortran libgomp libjava libobjc libssp .svn"
 
Old 05-16-2011, 12:42 AM   #8
atomicdog
LQ Newbie
 
Registered: May 2009
Location: San Diego
Distribution: Ubuntu
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
...
Code:
x="'hello this is quoted'"

echo "$x"

myf "$x"
As long as x is passed quoted it retains the internal quotes. Would someone explain if we are talking about something else?
Yeah this is what I wanted, Thanks.
I thought I tried that already.
I think I got so frustrated trying different things that I wasn't keeping track of exactly what I tried already. I probable tried passing it with the quotes but the variable had only single/double quote(which doesn't work) instead of having the double quotes wrapping the single quotes.
 
Old 05-16-2011, 02:49 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You can use the quotes in either direction, ie. "'blah'" or '"blah"', but of course the second will not expand variables, although some times this
can be the desired affect
 
1 members found this post helpful.
  


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
bash script position parameter does is not parsing '$1' junust Programming 5 04-08-2010 07:50 PM
bash: substitute parameter in a quoted string, stored in another variable GamerX Linux - General 5 01-31-2010 08:48 PM
Problem with bash script - variable name within variable name steven.c.banks Linux - Newbie 3 03-10-2009 03:08 AM
Conditional (IF) test in bash sh script - presence of first parameter Critcho Linux - Newbie 6 10-01-2008 12:20 AM
Bash variable problem: cURL -b parameter (string form e.g. "name=value;n2=v2") sithemac Other *NIX 3 07-09-2008 06:15 PM

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

All times are GMT -5. The time now is 11:20 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