LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 12-02-2015, 12:19 AM   #1
sluge
Member
 
Registered: Dec 2006
Location: Russia,52
Posts: 116

Rep: Reputation: 6
Question Why use doube quotas before backticks?


Hello,

I found a code where all variables a set by the following way:

VAR=""`ls -1`

Why "" put before the command?
 
Old 12-02-2015, 12:57 AM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,029
Blog Entries: 1

Rep: Reputation: 2114Reputation: 2114Reputation: 2114Reputation: 2114Reputation: 2114Reputation: 2114Reputation: 2114Reputation: 2114Reputation: 2114Reputation: 2114Reputation: 2114
Only the person who wrote it can say.

It is mostly harmless, perhaps they intended to prepend a title or something in the quotes.

There is no programmatic reason for doing so that I know of.
 
Old 12-02-2015, 05:57 AM   #3
Habitual
LQ Addict
 
Registered: Jan 2011
Posts: 7,679
Blog Entries: 10

Rep: Reputation: 2080Reputation: 2080Reputation: 2080Reputation: 2080Reputation: 2080Reputation: 2080Reputation: 2080Reputation: 2080Reputation: 2080Reputation: 2080Reputation: 2080
n/m
 
Old 12-02-2015, 01:10 PM   #4
debguy
Member
 
Registered: Oct 2014
Location: U.S.A.
Distribution: mixed, mostly debian slackare today
Posts: 207

Rep: Reputation: 19
Quote:
Originally Posted by sluge View Post
Hello,

I found a code where all variables a set by the following way:

VAR=""`ls -1`

Why "" put before the command?
the "" tells bash that what is inside is NOT TO BE EXPANDED


ALWAYS use quotes for strings and assignments unless your seeking planned flawless command substitution

see man bash(1) about expansion rules

consider:

VAR=`ls -1`

ls -1 may product "filenames with spaces" then you'd have

VAR=foo foo2

that's illegal, bash complains (if set -e is used, bash quits, see man bash(1))

now it's also possible a filename contains bad things like ! (exclamation mark, which bash thinks is special)

VAR=!foo

a disaster may happen

filename may contain $ and if name is $x

VAR=$x

VAR becomes the value of variable x - defintely not a good idea

VAR="$x" becomes variable x, yes, but bash does not apply commandline expantion to it, which can be a disaster. "$x" tells bash only certain expansions are allowed.

$ VAR=$x
$ VAR="$x"

AGAIN, may be the same: but may be disasterous without ""

VAR=`echo foo | tr -c "o" "i"` is a shortcut and often VAR=`ls` is shown in EXAMPLE scripts which omit "better practices" so to look thinner and be more clear. it's a shortcut but dont do it.

do you know if there are macros define in the envrionment that may be triggered? did you run the shell script in it's own invironment? did it inheirit what macro or global environment ? there are many more questions to ask about unquoted "full bash commandline substitution process" before running around without quotes

------------------------
there are too many more thigs that can go bad or really bad

in general:

ALWAYS use quotes for strings and assignments unless your seeking command substitution

glob * expands to '*' not '' if nothing is found, so be careful

even turning off globs is a safe practice

Last edited by debguy; 12-02-2015 at 01:18 PM.
 
Old 12-02-2015, 01:13 PM   #5
debguy
Member
 
Registered: Oct 2014
Location: U.S.A.
Distribution: mixed, mostly debian slackare today
Posts: 207

Rep: Reputation: 19
btw: this is valid in bash. in your example you didnt' show the ending quote.

that's either just a typo or intended to have an "embedded \n" (embedded line feed, see man bash)

$ VAR="hi" ; echo "$VAR"
hi
$ VAR="`ls -1`

hello

"

$ echo $VAR
file1 file2

hello

----------------
do you see the vertical whitespace ?
 
Old 12-02-2015, 01:15 PM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 7,164

Rep: Reputation: 2614Reputation: 2614Reputation: 2614Reputation: 2614Reputation: 2614Reputation: 2614Reputation: 2614Reputation: 2614Reputation: 2614Reputation: 2614Reputation: 2614
I think the empty string before the backtics was a mistake and nothing more.

A better and safer way to write it would be:

Code:
VAR="$(ls -1)"
 
Old 12-02-2015, 01:47 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,362

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
Quote:
Originally Posted by debguy View Post
VAR=`ls -1`

ls -1 may product "filenames with spaces" then you'd have

VAR=foo foo2

that's illegal, bash complains (if set -e is used, bash quits, see man bash(1))
No

Quote:
Originally Posted by debguy View Post
now it's also possible a filename contains bad things like ! (exclamation mark, which bash thinks is special)

VAR=!foo

a disaster may happen
No

Quote:
Originally Posted by debguy View Post
filename may contain $ and if name is $x

VAR=$x

VAR becomes the value of variable x - defintely not a good idea
No




All the above are wrong.
Code:
$ touch file1 file2
$ ls -1
file1
file2
$ var=`ls -1`
$ echo $var
file1 file2
$
$ rm *
$
$ touch \!file
$ ls -1
!file
$ var=`ls -1`
$ echo $var
!file
$
$ rm *
$
$ touch \$file
$ ls -1
$file
$ file=stuff
$ var=`ls -1`
$ echo $var
$file
It is completely unnecessary to stick quotes around command substitutions. Not only that, but the OP's example didn't even have quotes around the command substitution, it had quotes IN FRONT OF it, which is nothing more than a harmless typo.

Last edited by suicidaleggroll; 12-02-2015 at 01:49 PM.
 
Old 12-02-2015, 02:16 PM   #8
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,362

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
Also...

Quote:
Originally Posted by debguy View Post
the "" tells bash that what is inside is NOT TO BE EXPANDED
No it doesn't, you need single quotes for that.

Quote:
Originally Posted by debguy View Post
$ VAR=$x
$ VAR="$x"

AGAIN, may be the same: but may be disasterous without ""
Please provide a single example where this is true.
 
  


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
Using Backticks in Shell Scripts Woodsman Slackware 3 12-25-2009 11:32 AM
export & Backticks openSourceLover Programming 1 05-04-2009 03:45 AM
ssh hangs when using it in backticks in perl exceed1 Programming 10 01-17-2009 01:06 PM
Perl: Not all output is captured by backticks Poetics Programming 2 07-31-2007 04:40 PM
can you nest backticks? BrianK Programming 7 03-22-2007 01:40 PM


All times are GMT -5. The time now is 07:31 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration