LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 02-20-2009, 03:50 PM   #1
glinux
LQ Newbie
 
Registered: Feb 2009
Posts: 7

Rep: Reputation: 0
Question when to quote or not to quote variable names - mktemp problem


Hi,

double quotes in a variable name causes problems for mktemp when run from a shell script; in particular:

# following fails: quotes in $parent
parent="~/tmp"
mktemp -d -p $parent prefix.XXXX

# following fails, even though $grandparent that is assigned to $parent,
# is not explicitly quoted
grandparent="~/tmp"
parent=$grandparent
mktemp -d -p $parent prefix.XXXX

# removing quotes fixes problem,
grandparent=~/tmp
parent=$grandparent
mktemp -d -p $parent prefix.XXXX

but the solution negates the reason for quoting variables - a standard convention when scripting.

could someone explain why the quoted values for the variables causes mktemp to fail? and is this specific to mktemp? i haven't tried this for single quotes.

glinux
 
Old 02-20-2009, 04:02 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
The problem here is the order of the shell expansion. The variable expansion is done after tilde expansion, so that when the $parent is expanded to its value, the tilde remains literal. You can use the eval command to perform a further expansion. Removing the double quotes let the shell to expand the value of $parent immediately during the assignment.
 
Old 02-20-2009, 04:20 PM   #3
glinux
LQ Newbie
 
Registered: Feb 2009
Posts: 7

Original Poster
Rep: Reputation: 0
thanks for explanation

so there's two expansions that should happen for the variable to resolve to it's expected value: there's the variable expansion or substitution which results in a literal tilde and secondly, a tilde expansion, which is inhibited by the double quotes.

glinux
 
Old 02-20-2009, 04:37 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Correct. The quoting prevents reinterpretation of all special characters within the quoted string, except $, ` (backquote), and \ (escape) (cited from the Advanced Bash Scripting Guide). So the ~ in ~/tmp is not expanded and when passed to the mktemp command the variable substitution is performed with no possibility for the tilde expansion (due to the order of the expansion as explained above).

Of course, this behaviour is common to all the commands (not specific to mktemp). For example, consider the following:
Code:
$ pwd
/home/colucix/test
$ my_dir="~"
$ cd $my_dir
bash: cd: ~: No such file or directory
$ pwd
/home/colucix/test
$ my_dir=~
$ cd $my_dir
$ pwd
/home/colucix
 
  


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
quote "SERVICE variable was not set" error davimint Linux From Scratch 1 02-21-2008 12:57 AM
Quote 1 gb to 1 user? itz2000 Linux - Newbie 5 02-19-2006 02:30 PM
Quote at startup contrasutra Slackware 3 06-09-2003 04:18 AM
quote tundra General 2 09-21-2002 05:58 AM
Interesting MS quote cli_man General 7 05-14-2002 01:37 PM


All times are GMT -5. The time now is 11:09 PM.

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