Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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$
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.