LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 12-01-2015, 11:19 PM   #1
sluge
Member
 
Registered: Dec 2006
Location: Russia,52
Posts: 128

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-01-2015, 11:57 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
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, 04:57 AM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
n/m
 
Old 12-02-2015, 12: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 12:18 PM.
 
Old 12-02-2015, 12: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, 12:15 PM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
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, 12:47 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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 12:49 PM.
 
Old 12-02-2015, 01:16 PM   #8
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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



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

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

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