Variable with quotation marks “$()” The 2019 Stack Overflow Developer Survey Results Are...

Would an alien lifeform be able to achieve space travel if lacking in vision?

Why can't devices on different VLANs, but on the same subnet, communicate?

Mortgage adviser recommends a longer term than necessary combined with overpayments

Didn't get enough time to take a Coding Test - what to do now?

I could not break this equation. Please help me

Can the prologue be the backstory of your main character?

How to stretch delimiters to envolve matrices inside of a kbordermatrix?

Derivation tree not rendering

What was the last x86 CPU that did not have the x87 floating-point unit built in?

Did the UK government pay "millions and millions of dollars" to try to snag Julian Assange?

ELI5: Why do they say that Israel would have been the fourth country to land a spacecraft on the Moon and why do they call it low cost?

How should I replace vector<uint8_t>::const_iterator in an API?

Can withdrawing asylum be illegal?

What's the point in a preamp?

Semisimplicity of the category of coherent sheaves?

Segmentation fault output is suppressed when piping stdin into a function. Why?

Why does the Event Horizon Telescope (EHT) not include telescopes from Africa, Asia or Australia?

Was credit for the black hole image misattributed?

How does ice melt when immersed in water

Match Roman Numerals

How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time

How did passengers keep warm on sail ships?

Am I ethically obligated to go into work on an off day if the reason is sudden?

Change bounding box of math glyphs in LuaTeX



Variable with quotation marks “$()”



The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)What is the point of the bash Null-operator “:”, colon?Why does bash remove n in $(cat file)?What other bash variables are available during execution such as $USER that can assist on my script?Problem with script substitution when running scriptChange Gsetting with script on LogoutHow to link a custom keyboard shortcut to a bash script in Ubuntu 13.04?Can you help me to understand this explanation of shell quoting?Installing breach browserhow to fix my keyboard after a bash script messed it up[Edited/include]man: bash. e and bash shell “enter” tokenusing variable within quotation marksHow to get BASH to use * wildcard in command?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







12















I wrote this script :



#!/bin/bash
while [ true ]
do
currentoutput="$(lsusb)"
if [ "$currentoutput" != "$lastoutput" ]
then
echo "" date and Time >> test.log
date +%x_r >> test.log
lastoutput="$(lsusb)"
lsusb >> test.log
fi
sleep 5
done


I'm a newbie trying to learn fast and I got a question about the variable’s quotation marks.



Put a variable between $(), I get it, but why are the quotation marks needed even in the if statement? Is it to make a nested command?










share|improve this question









New contributor




Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 5





    Note that while [ true ] does produce an infinite loop, but perhaps not for the reason you think it does; while [ false ] also produces an infinite loop, because with a single argument, [ ... ] succeeds if that argument is a non-empty string. while true will actually run a command named true (which always succeeds).

    – chepner
    yesterday






  • 3





    You don't put a variable inside $(). You put a command inside $().

    – Wildcard
    yesterday


















12















I wrote this script :



#!/bin/bash
while [ true ]
do
currentoutput="$(lsusb)"
if [ "$currentoutput" != "$lastoutput" ]
then
echo "" date and Time >> test.log
date +%x_r >> test.log
lastoutput="$(lsusb)"
lsusb >> test.log
fi
sleep 5
done


I'm a newbie trying to learn fast and I got a question about the variable’s quotation marks.



Put a variable between $(), I get it, but why are the quotation marks needed even in the if statement? Is it to make a nested command?










share|improve this question









New contributor




Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 5





    Note that while [ true ] does produce an infinite loop, but perhaps not for the reason you think it does; while [ false ] also produces an infinite loop, because with a single argument, [ ... ] succeeds if that argument is a non-empty string. while true will actually run a command named true (which always succeeds).

    – chepner
    yesterday






  • 3





    You don't put a variable inside $(). You put a command inside $().

    – Wildcard
    yesterday














12












12








12


1






I wrote this script :



#!/bin/bash
while [ true ]
do
currentoutput="$(lsusb)"
if [ "$currentoutput" != "$lastoutput" ]
then
echo "" date and Time >> test.log
date +%x_r >> test.log
lastoutput="$(lsusb)"
lsusb >> test.log
fi
sleep 5
done


I'm a newbie trying to learn fast and I got a question about the variable’s quotation marks.



Put a variable between $(), I get it, but why are the quotation marks needed even in the if statement? Is it to make a nested command?










share|improve this question









New contributor




Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I wrote this script :



#!/bin/bash
while [ true ]
do
currentoutput="$(lsusb)"
if [ "$currentoutput" != "$lastoutput" ]
then
echo "" date and Time >> test.log
date +%x_r >> test.log
lastoutput="$(lsusb)"
lsusb >> test.log
fi
sleep 5
done


I'm a newbie trying to learn fast and I got a question about the variable’s quotation marks.



Put a variable between $(), I get it, but why are the quotation marks needed even in the if statement? Is it to make a nested command?







bash scripts






share|improve this question









New contributor




Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday









dessert

25.5k674108




25.5k674108






New contributor




Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









ShankharaShankhara

613




613




New contributor




Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 5





    Note that while [ true ] does produce an infinite loop, but perhaps not for the reason you think it does; while [ false ] also produces an infinite loop, because with a single argument, [ ... ] succeeds if that argument is a non-empty string. while true will actually run a command named true (which always succeeds).

    – chepner
    yesterday






  • 3





    You don't put a variable inside $(). You put a command inside $().

    – Wildcard
    yesterday














  • 5





    Note that while [ true ] does produce an infinite loop, but perhaps not for the reason you think it does; while [ false ] also produces an infinite loop, because with a single argument, [ ... ] succeeds if that argument is a non-empty string. while true will actually run a command named true (which always succeeds).

    – chepner
    yesterday






  • 3





    You don't put a variable inside $(). You put a command inside $().

    – Wildcard
    yesterday








5




5





Note that while [ true ] does produce an infinite loop, but perhaps not for the reason you think it does; while [ false ] also produces an infinite loop, because with a single argument, [ ... ] succeeds if that argument is a non-empty string. while true will actually run a command named true (which always succeeds).

– chepner
yesterday





Note that while [ true ] does produce an infinite loop, but perhaps not for the reason you think it does; while [ false ] also produces an infinite loop, because with a single argument, [ ... ] succeeds if that argument is a non-empty string. while true will actually run a command named true (which always succeeds).

– chepner
yesterday




3




3





You don't put a variable inside $(). You put a command inside $().

– Wildcard
yesterday





You don't put a variable inside $(). You put a command inside $().

– Wildcard
yesterday










4 Answers
4






active

oldest

votes


















21














Quotation marks prevent "word splitting". That is: breaking down variables into multiple items at whitespace characters (or to be more exact, at spaces, tabs, and newlines as defined in the value of the default $IFS shell variable).



For example,



$ var="one two"
$ howmany(){ echo $#; }
$ howmany $var
2
$ howmany "$var"
1


Here we define the howmany function which just lets us know how many positional parameters are given. As you can see, there are two items being passed to the variable, and with the quotes the text in the variable is treated as one unit.



This is important for accurate passing of information. For example, if the variable contains path to file, and the filename contains spaces anywhere in the path, the command you are trying to run may fail or give inaccurate results. If we were trying to create a file with the $var variable, touch $var would create two files, but touch "$var" just one.



Same goes for your [ "$currentoutput" != "$lastoutput" ] part. This particular test performs a comparison on two strings. When the test runs, the [ command would need to see 3 arguments - a text string, the != operator, and another text string. Keeping double quotes prevents word splitting, and the [ command sees exactly those 3 arguments. Now what happens if variables are unquoted ?



$ var="hello world"
$ foo="hi world"
$ [ $var != $foo ]
bash: [: too many arguments
$


Here, word splitting occurs, and instead [ sees two strings hello and world followed by !=, followed by two other strings hi world. Key point is that without double quotes, the contents of variables are understood as separate units rather than one whole item.



Assigning command substitution doesn't require double quotes as in



var=$( df )


where you have the df command's output saved to var. However, it is a good habit to always double quote variables and command substitution $(...) unless you do in fact want the output to be treated as separate items.





On a side note, the



while [ true ]


part can be



while true


[ is a command which evaluates its arguments, and [ whatever ] is always true regardless of what is inside. By contrast, while true uses the command true which always returns success exit status (and that's exactly what while loop needs). The difference is a bit more clarity and less testing performed. Alternatively, you could also use : instead of true



The double quotes in echo "" date and Time part could probably be removed. They merely insert an empty string and an add extra space to the output. If that's desired, feel free to keep them there, but there's no particular functional value in this case.



lsusb >> test.log


This part could probably be replaced with echo "$currentoutput" >> test.log. There's no reason to run lsusb again after it has been run already in currentoutput=$(lsusb). In cases where trailing newlines
have to be preserved in the output - one could see the value in running a command multiple times, but in case of lsusb there's no need for that. The less external commands you call, the better, because every call to a non-built-in command incurs costs in CPU, memory usage, and execution time ( even though the commands are probably pre-loaded from memory).





See also:




  • When is double-quoting necessary?

  • What is the point of the bash null operator : (colon)?

  • Security implications of forgetting to quote a variable in bash/POSIX shells






share|improve this answer


























  • Great answer, if you didn't already, you should write a book about bash. But in the last part shouldn't echo "$currentoutput" >> test.log better be printf '%sn' "$currentoutput" >> test.log ?

    – RoVo
    yesterday








  • 1





    @RoVo Yes, printf should be preferred for portability. Since we're using bash-specific script here it can be excused to use echo. But your observation is very much correct

    – Sergiy Kolodyazhnyy
    yesterday






  • 1





    @SergiyKolodyazhnyy, ...I'm not sure that echo is completely reliable even when the shell is known to be bash (and the value of the xpg_echo and posix flags is known); if your value could be -n or -e, it can disappear instead of being printed.

    – Charles Duffy
    yesterday





















3














In currentoutput="$(lsusb)" lsusb is not a variable, it is a command. What this statement does, it executes lsusb command and assigns its output to currentoutput variable.



Older syntax for this was



currentoutput=`lsusb`


you can find it in many examples and scripts



To answer the other part of your question, if [ ] is just how syntax for if is defined in bash. See more in https://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html






share|improve this answer



















  • 3





    I think it's important to say that [ ] is actually the test command. You can use if statements with other commands too, as it relies on a 0 or non-zero test of their exit code.

    – Arronical
    yesterday











  • ...indeed, it's much better to run if grep -qe "somestring" file than to run grep -qe "somestring" file; if [ $? = 0 ]; then ..., so the claim that if [ ... is part of the definition of if syntax is not just misleading but leads to bad practices.

    – Charles Duffy
    yesterday





















3














The following runs the external command command and returns its output.



"$(command)"


Without the brackets/parentheses, this would look for a variable instead of running a command:



"$variable"


As for the difference between $variable and "$variable", this becomes relevant when $variable contains spaces. When using "$variable", the entire variable contents will be inserted into a single string even if the contents include spaces. When using $variable the contents of the variable may be expanded into an argument list of multiple arguments.






share|improve this answer


























  • HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !

    – Shankhara
    yesterday



















0














To be contrarian - bash recommends you use the [[ ... ]] over the [ ... ] construct to avoid having to quote variables in the test and so the associated problems of word splitting that the others have pointed out.



[ is provided in bash for POSIX compatibility with scripts meant to run under #!/bin/sh or those being ported over to bash - for the most part, you should avoid it in favour of [[.



e.g.



# With [ - quotes are needed

$ foo='one two'; bar='one two'; [ $foo = $bar ] && echo "they're equal"
-bash: [: too many arguments

$ foo='one two'; bar='one two'; [ "$foo" = "$bar" ] && echo "they're equal"
they're equal

# versus [[ - quotes not needed

$ foo='one two'; bar='one two'; [[ $foo = $bar ]] && echo "they're equal"
they're equal






share|improve this answer
























  • bash makes no such recommendation; it provides [[ ... ]] which can be more convenient and has some functionality that [ ... ] does not, but there is no problem with using [ ... ] correctly.

    – chepner
    yesterday











  • @chepner - Maybe I should be clearer here. Sure it's not an explicit recommendation in the bash manpage but given [[ does everything [ does and more and still the many times [ trips people up and then try and retrofit features of [[ back to [ only to fail and leave bugs scattered about - it's fair to say it is a community recommendation in as far as most relevant bash style guides will advise never use [.

    – shalomb
    yesterday












Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Shankhara is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1133173%2fvariable-with-quotation-marks%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes









21














Quotation marks prevent "word splitting". That is: breaking down variables into multiple items at whitespace characters (or to be more exact, at spaces, tabs, and newlines as defined in the value of the default $IFS shell variable).



For example,



$ var="one two"
$ howmany(){ echo $#; }
$ howmany $var
2
$ howmany "$var"
1


Here we define the howmany function which just lets us know how many positional parameters are given. As you can see, there are two items being passed to the variable, and with the quotes the text in the variable is treated as one unit.



This is important for accurate passing of information. For example, if the variable contains path to file, and the filename contains spaces anywhere in the path, the command you are trying to run may fail or give inaccurate results. If we were trying to create a file with the $var variable, touch $var would create two files, but touch "$var" just one.



Same goes for your [ "$currentoutput" != "$lastoutput" ] part. This particular test performs a comparison on two strings. When the test runs, the [ command would need to see 3 arguments - a text string, the != operator, and another text string. Keeping double quotes prevents word splitting, and the [ command sees exactly those 3 arguments. Now what happens if variables are unquoted ?



$ var="hello world"
$ foo="hi world"
$ [ $var != $foo ]
bash: [: too many arguments
$


Here, word splitting occurs, and instead [ sees two strings hello and world followed by !=, followed by two other strings hi world. Key point is that without double quotes, the contents of variables are understood as separate units rather than one whole item.



Assigning command substitution doesn't require double quotes as in



var=$( df )


where you have the df command's output saved to var. However, it is a good habit to always double quote variables and command substitution $(...) unless you do in fact want the output to be treated as separate items.





On a side note, the



while [ true ]


part can be



while true


[ is a command which evaluates its arguments, and [ whatever ] is always true regardless of what is inside. By contrast, while true uses the command true which always returns success exit status (and that's exactly what while loop needs). The difference is a bit more clarity and less testing performed. Alternatively, you could also use : instead of true



The double quotes in echo "" date and Time part could probably be removed. They merely insert an empty string and an add extra space to the output. If that's desired, feel free to keep them there, but there's no particular functional value in this case.



lsusb >> test.log


This part could probably be replaced with echo "$currentoutput" >> test.log. There's no reason to run lsusb again after it has been run already in currentoutput=$(lsusb). In cases where trailing newlines
have to be preserved in the output - one could see the value in running a command multiple times, but in case of lsusb there's no need for that. The less external commands you call, the better, because every call to a non-built-in command incurs costs in CPU, memory usage, and execution time ( even though the commands are probably pre-loaded from memory).





See also:




  • When is double-quoting necessary?

  • What is the point of the bash null operator : (colon)?

  • Security implications of forgetting to quote a variable in bash/POSIX shells






share|improve this answer


























  • Great answer, if you didn't already, you should write a book about bash. But in the last part shouldn't echo "$currentoutput" >> test.log better be printf '%sn' "$currentoutput" >> test.log ?

    – RoVo
    yesterday








  • 1





    @RoVo Yes, printf should be preferred for portability. Since we're using bash-specific script here it can be excused to use echo. But your observation is very much correct

    – Sergiy Kolodyazhnyy
    yesterday






  • 1





    @SergiyKolodyazhnyy, ...I'm not sure that echo is completely reliable even when the shell is known to be bash (and the value of the xpg_echo and posix flags is known); if your value could be -n or -e, it can disappear instead of being printed.

    – Charles Duffy
    yesterday


















21














Quotation marks prevent "word splitting". That is: breaking down variables into multiple items at whitespace characters (or to be more exact, at spaces, tabs, and newlines as defined in the value of the default $IFS shell variable).



For example,



$ var="one two"
$ howmany(){ echo $#; }
$ howmany $var
2
$ howmany "$var"
1


Here we define the howmany function which just lets us know how many positional parameters are given. As you can see, there are two items being passed to the variable, and with the quotes the text in the variable is treated as one unit.



This is important for accurate passing of information. For example, if the variable contains path to file, and the filename contains spaces anywhere in the path, the command you are trying to run may fail or give inaccurate results. If we were trying to create a file with the $var variable, touch $var would create two files, but touch "$var" just one.



Same goes for your [ "$currentoutput" != "$lastoutput" ] part. This particular test performs a comparison on two strings. When the test runs, the [ command would need to see 3 arguments - a text string, the != operator, and another text string. Keeping double quotes prevents word splitting, and the [ command sees exactly those 3 arguments. Now what happens if variables are unquoted ?



$ var="hello world"
$ foo="hi world"
$ [ $var != $foo ]
bash: [: too many arguments
$


Here, word splitting occurs, and instead [ sees two strings hello and world followed by !=, followed by two other strings hi world. Key point is that without double quotes, the contents of variables are understood as separate units rather than one whole item.



Assigning command substitution doesn't require double quotes as in



var=$( df )


where you have the df command's output saved to var. However, it is a good habit to always double quote variables and command substitution $(...) unless you do in fact want the output to be treated as separate items.





On a side note, the



while [ true ]


part can be



while true


[ is a command which evaluates its arguments, and [ whatever ] is always true regardless of what is inside. By contrast, while true uses the command true which always returns success exit status (and that's exactly what while loop needs). The difference is a bit more clarity and less testing performed. Alternatively, you could also use : instead of true



The double quotes in echo "" date and Time part could probably be removed. They merely insert an empty string and an add extra space to the output. If that's desired, feel free to keep them there, but there's no particular functional value in this case.



lsusb >> test.log


This part could probably be replaced with echo "$currentoutput" >> test.log. There's no reason to run lsusb again after it has been run already in currentoutput=$(lsusb). In cases where trailing newlines
have to be preserved in the output - one could see the value in running a command multiple times, but in case of lsusb there's no need for that. The less external commands you call, the better, because every call to a non-built-in command incurs costs in CPU, memory usage, and execution time ( even though the commands are probably pre-loaded from memory).





See also:




  • When is double-quoting necessary?

  • What is the point of the bash null operator : (colon)?

  • Security implications of forgetting to quote a variable in bash/POSIX shells






share|improve this answer


























  • Great answer, if you didn't already, you should write a book about bash. But in the last part shouldn't echo "$currentoutput" >> test.log better be printf '%sn' "$currentoutput" >> test.log ?

    – RoVo
    yesterday








  • 1





    @RoVo Yes, printf should be preferred for portability. Since we're using bash-specific script here it can be excused to use echo. But your observation is very much correct

    – Sergiy Kolodyazhnyy
    yesterday






  • 1





    @SergiyKolodyazhnyy, ...I'm not sure that echo is completely reliable even when the shell is known to be bash (and the value of the xpg_echo and posix flags is known); if your value could be -n or -e, it can disappear instead of being printed.

    – Charles Duffy
    yesterday
















21












21








21







Quotation marks prevent "word splitting". That is: breaking down variables into multiple items at whitespace characters (or to be more exact, at spaces, tabs, and newlines as defined in the value of the default $IFS shell variable).



For example,



$ var="one two"
$ howmany(){ echo $#; }
$ howmany $var
2
$ howmany "$var"
1


Here we define the howmany function which just lets us know how many positional parameters are given. As you can see, there are two items being passed to the variable, and with the quotes the text in the variable is treated as one unit.



This is important for accurate passing of information. For example, if the variable contains path to file, and the filename contains spaces anywhere in the path, the command you are trying to run may fail or give inaccurate results. If we were trying to create a file with the $var variable, touch $var would create two files, but touch "$var" just one.



Same goes for your [ "$currentoutput" != "$lastoutput" ] part. This particular test performs a comparison on two strings. When the test runs, the [ command would need to see 3 arguments - a text string, the != operator, and another text string. Keeping double quotes prevents word splitting, and the [ command sees exactly those 3 arguments. Now what happens if variables are unquoted ?



$ var="hello world"
$ foo="hi world"
$ [ $var != $foo ]
bash: [: too many arguments
$


Here, word splitting occurs, and instead [ sees two strings hello and world followed by !=, followed by two other strings hi world. Key point is that without double quotes, the contents of variables are understood as separate units rather than one whole item.



Assigning command substitution doesn't require double quotes as in



var=$( df )


where you have the df command's output saved to var. However, it is a good habit to always double quote variables and command substitution $(...) unless you do in fact want the output to be treated as separate items.





On a side note, the



while [ true ]


part can be



while true


[ is a command which evaluates its arguments, and [ whatever ] is always true regardless of what is inside. By contrast, while true uses the command true which always returns success exit status (and that's exactly what while loop needs). The difference is a bit more clarity and less testing performed. Alternatively, you could also use : instead of true



The double quotes in echo "" date and Time part could probably be removed. They merely insert an empty string and an add extra space to the output. If that's desired, feel free to keep them there, but there's no particular functional value in this case.



lsusb >> test.log


This part could probably be replaced with echo "$currentoutput" >> test.log. There's no reason to run lsusb again after it has been run already in currentoutput=$(lsusb). In cases where trailing newlines
have to be preserved in the output - one could see the value in running a command multiple times, but in case of lsusb there's no need for that. The less external commands you call, the better, because every call to a non-built-in command incurs costs in CPU, memory usage, and execution time ( even though the commands are probably pre-loaded from memory).





See also:




  • When is double-quoting necessary?

  • What is the point of the bash null operator : (colon)?

  • Security implications of forgetting to quote a variable in bash/POSIX shells






share|improve this answer















Quotation marks prevent "word splitting". That is: breaking down variables into multiple items at whitespace characters (or to be more exact, at spaces, tabs, and newlines as defined in the value of the default $IFS shell variable).



For example,



$ var="one two"
$ howmany(){ echo $#; }
$ howmany $var
2
$ howmany "$var"
1


Here we define the howmany function which just lets us know how many positional parameters are given. As you can see, there are two items being passed to the variable, and with the quotes the text in the variable is treated as one unit.



This is important for accurate passing of information. For example, if the variable contains path to file, and the filename contains spaces anywhere in the path, the command you are trying to run may fail or give inaccurate results. If we were trying to create a file with the $var variable, touch $var would create two files, but touch "$var" just one.



Same goes for your [ "$currentoutput" != "$lastoutput" ] part. This particular test performs a comparison on two strings. When the test runs, the [ command would need to see 3 arguments - a text string, the != operator, and another text string. Keeping double quotes prevents word splitting, and the [ command sees exactly those 3 arguments. Now what happens if variables are unquoted ?



$ var="hello world"
$ foo="hi world"
$ [ $var != $foo ]
bash: [: too many arguments
$


Here, word splitting occurs, and instead [ sees two strings hello and world followed by !=, followed by two other strings hi world. Key point is that without double quotes, the contents of variables are understood as separate units rather than one whole item.



Assigning command substitution doesn't require double quotes as in



var=$( df )


where you have the df command's output saved to var. However, it is a good habit to always double quote variables and command substitution $(...) unless you do in fact want the output to be treated as separate items.





On a side note, the



while [ true ]


part can be



while true


[ is a command which evaluates its arguments, and [ whatever ] is always true regardless of what is inside. By contrast, while true uses the command true which always returns success exit status (and that's exactly what while loop needs). The difference is a bit more clarity and less testing performed. Alternatively, you could also use : instead of true



The double quotes in echo "" date and Time part could probably be removed. They merely insert an empty string and an add extra space to the output. If that's desired, feel free to keep them there, but there's no particular functional value in this case.



lsusb >> test.log


This part could probably be replaced with echo "$currentoutput" >> test.log. There's no reason to run lsusb again after it has been run already in currentoutput=$(lsusb). In cases where trailing newlines
have to be preserved in the output - one could see the value in running a command multiple times, but in case of lsusb there's no need for that. The less external commands you call, the better, because every call to a non-built-in command incurs costs in CPU, memory usage, and execution time ( even though the commands are probably pre-loaded from memory).





See also:




  • When is double-quoting necessary?

  • What is the point of the bash null operator : (colon)?

  • Security implications of forgetting to quote a variable in bash/POSIX shells







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday









terdon

67.8k13139223




67.8k13139223










answered yesterday









Sergiy KolodyazhnyySergiy Kolodyazhnyy

75.4k9155328




75.4k9155328













  • Great answer, if you didn't already, you should write a book about bash. But in the last part shouldn't echo "$currentoutput" >> test.log better be printf '%sn' "$currentoutput" >> test.log ?

    – RoVo
    yesterday








  • 1





    @RoVo Yes, printf should be preferred for portability. Since we're using bash-specific script here it can be excused to use echo. But your observation is very much correct

    – Sergiy Kolodyazhnyy
    yesterday






  • 1





    @SergiyKolodyazhnyy, ...I'm not sure that echo is completely reliable even when the shell is known to be bash (and the value of the xpg_echo and posix flags is known); if your value could be -n or -e, it can disappear instead of being printed.

    – Charles Duffy
    yesterday





















  • Great answer, if you didn't already, you should write a book about bash. But in the last part shouldn't echo "$currentoutput" >> test.log better be printf '%sn' "$currentoutput" >> test.log ?

    – RoVo
    yesterday








  • 1





    @RoVo Yes, printf should be preferred for portability. Since we're using bash-specific script here it can be excused to use echo. But your observation is very much correct

    – Sergiy Kolodyazhnyy
    yesterday






  • 1





    @SergiyKolodyazhnyy, ...I'm not sure that echo is completely reliable even when the shell is known to be bash (and the value of the xpg_echo and posix flags is known); if your value could be -n or -e, it can disappear instead of being printed.

    – Charles Duffy
    yesterday



















Great answer, if you didn't already, you should write a book about bash. But in the last part shouldn't echo "$currentoutput" >> test.log better be printf '%sn' "$currentoutput" >> test.log ?

– RoVo
yesterday







Great answer, if you didn't already, you should write a book about bash. But in the last part shouldn't echo "$currentoutput" >> test.log better be printf '%sn' "$currentoutput" >> test.log ?

– RoVo
yesterday






1




1





@RoVo Yes, printf should be preferred for portability. Since we're using bash-specific script here it can be excused to use echo. But your observation is very much correct

– Sergiy Kolodyazhnyy
yesterday





@RoVo Yes, printf should be preferred for portability. Since we're using bash-specific script here it can be excused to use echo. But your observation is very much correct

– Sergiy Kolodyazhnyy
yesterday




1




1





@SergiyKolodyazhnyy, ...I'm not sure that echo is completely reliable even when the shell is known to be bash (and the value of the xpg_echo and posix flags is known); if your value could be -n or -e, it can disappear instead of being printed.

– Charles Duffy
yesterday







@SergiyKolodyazhnyy, ...I'm not sure that echo is completely reliable even when the shell is known to be bash (and the value of the xpg_echo and posix flags is known); if your value could be -n or -e, it can disappear instead of being printed.

– Charles Duffy
yesterday















3














In currentoutput="$(lsusb)" lsusb is not a variable, it is a command. What this statement does, it executes lsusb command and assigns its output to currentoutput variable.



Older syntax for this was



currentoutput=`lsusb`


you can find it in many examples and scripts



To answer the other part of your question, if [ ] is just how syntax for if is defined in bash. See more in https://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html






share|improve this answer



















  • 3





    I think it's important to say that [ ] is actually the test command. You can use if statements with other commands too, as it relies on a 0 or non-zero test of their exit code.

    – Arronical
    yesterday











  • ...indeed, it's much better to run if grep -qe "somestring" file than to run grep -qe "somestring" file; if [ $? = 0 ]; then ..., so the claim that if [ ... is part of the definition of if syntax is not just misleading but leads to bad practices.

    – Charles Duffy
    yesterday


















3














In currentoutput="$(lsusb)" lsusb is not a variable, it is a command. What this statement does, it executes lsusb command and assigns its output to currentoutput variable.



Older syntax for this was



currentoutput=`lsusb`


you can find it in many examples and scripts



To answer the other part of your question, if [ ] is just how syntax for if is defined in bash. See more in https://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html






share|improve this answer



















  • 3





    I think it's important to say that [ ] is actually the test command. You can use if statements with other commands too, as it relies on a 0 or non-zero test of their exit code.

    – Arronical
    yesterday











  • ...indeed, it's much better to run if grep -qe "somestring" file than to run grep -qe "somestring" file; if [ $? = 0 ]; then ..., so the claim that if [ ... is part of the definition of if syntax is not just misleading but leads to bad practices.

    – Charles Duffy
    yesterday
















3












3








3







In currentoutput="$(lsusb)" lsusb is not a variable, it is a command. What this statement does, it executes lsusb command and assigns its output to currentoutput variable.



Older syntax for this was



currentoutput=`lsusb`


you can find it in many examples and scripts



To answer the other part of your question, if [ ] is just how syntax for if is defined in bash. See more in https://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html






share|improve this answer













In currentoutput="$(lsusb)" lsusb is not a variable, it is a command. What this statement does, it executes lsusb command and assigns its output to currentoutput variable.



Older syntax for this was



currentoutput=`lsusb`


you can find it in many examples and scripts



To answer the other part of your question, if [ ] is just how syntax for if is defined in bash. See more in https://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html







share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









marosgmarosg

47437




47437








  • 3





    I think it's important to say that [ ] is actually the test command. You can use if statements with other commands too, as it relies on a 0 or non-zero test of their exit code.

    – Arronical
    yesterday











  • ...indeed, it's much better to run if grep -qe "somestring" file than to run grep -qe "somestring" file; if [ $? = 0 ]; then ..., so the claim that if [ ... is part of the definition of if syntax is not just misleading but leads to bad practices.

    – Charles Duffy
    yesterday
















  • 3





    I think it's important to say that [ ] is actually the test command. You can use if statements with other commands too, as it relies on a 0 or non-zero test of their exit code.

    – Arronical
    yesterday











  • ...indeed, it's much better to run if grep -qe "somestring" file than to run grep -qe "somestring" file; if [ $? = 0 ]; then ..., so the claim that if [ ... is part of the definition of if syntax is not just misleading but leads to bad practices.

    – Charles Duffy
    yesterday










3




3





I think it's important to say that [ ] is actually the test command. You can use if statements with other commands too, as it relies on a 0 or non-zero test of their exit code.

– Arronical
yesterday





I think it's important to say that [ ] is actually the test command. You can use if statements with other commands too, as it relies on a 0 or non-zero test of their exit code.

– Arronical
yesterday













...indeed, it's much better to run if grep -qe "somestring" file than to run grep -qe "somestring" file; if [ $? = 0 ]; then ..., so the claim that if [ ... is part of the definition of if syntax is not just misleading but leads to bad practices.

– Charles Duffy
yesterday







...indeed, it's much better to run if grep -qe "somestring" file than to run grep -qe "somestring" file; if [ $? = 0 ]; then ..., so the claim that if [ ... is part of the definition of if syntax is not just misleading but leads to bad practices.

– Charles Duffy
yesterday













3














The following runs the external command command and returns its output.



"$(command)"


Without the brackets/parentheses, this would look for a variable instead of running a command:



"$variable"


As for the difference between $variable and "$variable", this becomes relevant when $variable contains spaces. When using "$variable", the entire variable contents will be inserted into a single string even if the contents include spaces. When using $variable the contents of the variable may be expanded into an argument list of multiple arguments.






share|improve this answer


























  • HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !

    – Shankhara
    yesterday
















3














The following runs the external command command and returns its output.



"$(command)"


Without the brackets/parentheses, this would look for a variable instead of running a command:



"$variable"


As for the difference between $variable and "$variable", this becomes relevant when $variable contains spaces. When using "$variable", the entire variable contents will be inserted into a single string even if the contents include spaces. When using $variable the contents of the variable may be expanded into an argument list of multiple arguments.






share|improve this answer


























  • HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !

    – Shankhara
    yesterday














3












3








3







The following runs the external command command and returns its output.



"$(command)"


Without the brackets/parentheses, this would look for a variable instead of running a command:



"$variable"


As for the difference between $variable and "$variable", this becomes relevant when $variable contains spaces. When using "$variable", the entire variable contents will be inserted into a single string even if the contents include spaces. When using $variable the contents of the variable may be expanded into an argument list of multiple arguments.






share|improve this answer















The following runs the external command command and returns its output.



"$(command)"


Without the brackets/parentheses, this would look for a variable instead of running a command:



"$variable"


As for the difference between $variable and "$variable", this becomes relevant when $variable contains spaces. When using "$variable", the entire variable contents will be inserted into a single string even if the contents include spaces. When using $variable the contents of the variable may be expanded into an argument list of multiple arguments.







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered yesterday









thomasrutterthomasrutter

27.3k47089




27.3k47089













  • HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !

    – Shankhara
    yesterday



















  • HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !

    – Shankhara
    yesterday

















HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !

– Shankhara
yesterday





HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !

– Shankhara
yesterday











0














To be contrarian - bash recommends you use the [[ ... ]] over the [ ... ] construct to avoid having to quote variables in the test and so the associated problems of word splitting that the others have pointed out.



[ is provided in bash for POSIX compatibility with scripts meant to run under #!/bin/sh or those being ported over to bash - for the most part, you should avoid it in favour of [[.



e.g.



# With [ - quotes are needed

$ foo='one two'; bar='one two'; [ $foo = $bar ] && echo "they're equal"
-bash: [: too many arguments

$ foo='one two'; bar='one two'; [ "$foo" = "$bar" ] && echo "they're equal"
they're equal

# versus [[ - quotes not needed

$ foo='one two'; bar='one two'; [[ $foo = $bar ]] && echo "they're equal"
they're equal






share|improve this answer
























  • bash makes no such recommendation; it provides [[ ... ]] which can be more convenient and has some functionality that [ ... ] does not, but there is no problem with using [ ... ] correctly.

    – chepner
    yesterday











  • @chepner - Maybe I should be clearer here. Sure it's not an explicit recommendation in the bash manpage but given [[ does everything [ does and more and still the many times [ trips people up and then try and retrofit features of [[ back to [ only to fail and leave bugs scattered about - it's fair to say it is a community recommendation in as far as most relevant bash style guides will advise never use [.

    – shalomb
    yesterday
















0














To be contrarian - bash recommends you use the [[ ... ]] over the [ ... ] construct to avoid having to quote variables in the test and so the associated problems of word splitting that the others have pointed out.



[ is provided in bash for POSIX compatibility with scripts meant to run under #!/bin/sh or those being ported over to bash - for the most part, you should avoid it in favour of [[.



e.g.



# With [ - quotes are needed

$ foo='one two'; bar='one two'; [ $foo = $bar ] && echo "they're equal"
-bash: [: too many arguments

$ foo='one two'; bar='one two'; [ "$foo" = "$bar" ] && echo "they're equal"
they're equal

# versus [[ - quotes not needed

$ foo='one two'; bar='one two'; [[ $foo = $bar ]] && echo "they're equal"
they're equal






share|improve this answer
























  • bash makes no such recommendation; it provides [[ ... ]] which can be more convenient and has some functionality that [ ... ] does not, but there is no problem with using [ ... ] correctly.

    – chepner
    yesterday











  • @chepner - Maybe I should be clearer here. Sure it's not an explicit recommendation in the bash manpage but given [[ does everything [ does and more and still the many times [ trips people up and then try and retrofit features of [[ back to [ only to fail and leave bugs scattered about - it's fair to say it is a community recommendation in as far as most relevant bash style guides will advise never use [.

    – shalomb
    yesterday














0












0








0







To be contrarian - bash recommends you use the [[ ... ]] over the [ ... ] construct to avoid having to quote variables in the test and so the associated problems of word splitting that the others have pointed out.



[ is provided in bash for POSIX compatibility with scripts meant to run under #!/bin/sh or those being ported over to bash - for the most part, you should avoid it in favour of [[.



e.g.



# With [ - quotes are needed

$ foo='one two'; bar='one two'; [ $foo = $bar ] && echo "they're equal"
-bash: [: too many arguments

$ foo='one two'; bar='one two'; [ "$foo" = "$bar" ] && echo "they're equal"
they're equal

# versus [[ - quotes not needed

$ foo='one two'; bar='one two'; [[ $foo = $bar ]] && echo "they're equal"
they're equal






share|improve this answer













To be contrarian - bash recommends you use the [[ ... ]] over the [ ... ] construct to avoid having to quote variables in the test and so the associated problems of word splitting that the others have pointed out.



[ is provided in bash for POSIX compatibility with scripts meant to run under #!/bin/sh or those being ported over to bash - for the most part, you should avoid it in favour of [[.



e.g.



# With [ - quotes are needed

$ foo='one two'; bar='one two'; [ $foo = $bar ] && echo "they're equal"
-bash: [: too many arguments

$ foo='one two'; bar='one two'; [ "$foo" = "$bar" ] && echo "they're equal"
they're equal

# versus [[ - quotes not needed

$ foo='one two'; bar='one two'; [[ $foo = $bar ]] && echo "they're equal"
they're equal







share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









shalombshalomb

1814




1814













  • bash makes no such recommendation; it provides [[ ... ]] which can be more convenient and has some functionality that [ ... ] does not, but there is no problem with using [ ... ] correctly.

    – chepner
    yesterday











  • @chepner - Maybe I should be clearer here. Sure it's not an explicit recommendation in the bash manpage but given [[ does everything [ does and more and still the many times [ trips people up and then try and retrofit features of [[ back to [ only to fail and leave bugs scattered about - it's fair to say it is a community recommendation in as far as most relevant bash style guides will advise never use [.

    – shalomb
    yesterday



















  • bash makes no such recommendation; it provides [[ ... ]] which can be more convenient and has some functionality that [ ... ] does not, but there is no problem with using [ ... ] correctly.

    – chepner
    yesterday











  • @chepner - Maybe I should be clearer here. Sure it's not an explicit recommendation in the bash manpage but given [[ does everything [ does and more and still the many times [ trips people up and then try and retrofit features of [[ back to [ only to fail and leave bugs scattered about - it's fair to say it is a community recommendation in as far as most relevant bash style guides will advise never use [.

    – shalomb
    yesterday

















bash makes no such recommendation; it provides [[ ... ]] which can be more convenient and has some functionality that [ ... ] does not, but there is no problem with using [ ... ] correctly.

– chepner
yesterday





bash makes no such recommendation; it provides [[ ... ]] which can be more convenient and has some functionality that [ ... ] does not, but there is no problem with using [ ... ] correctly.

– chepner
yesterday













@chepner - Maybe I should be clearer here. Sure it's not an explicit recommendation in the bash manpage but given [[ does everything [ does and more and still the many times [ trips people up and then try and retrofit features of [[ back to [ only to fail and leave bugs scattered about - it's fair to say it is a community recommendation in as far as most relevant bash style guides will advise never use [.

– shalomb
yesterday





@chepner - Maybe I should be clearer here. Sure it's not an explicit recommendation in the bash manpage but given [[ does everything [ does and more and still the many times [ trips people up and then try and retrofit features of [[ back to [ only to fail and leave bugs scattered about - it's fair to say it is a community recommendation in as far as most relevant bash style guides will advise never use [.

– shalomb
yesterday










Shankhara is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Shankhara is a new contributor. Be nice, and check out our Code of Conduct.













Shankhara is a new contributor. Be nice, and check out our Code of Conduct.












Shankhara is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Ask Ubuntu!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1133173%2fvariable-with-quotation-marks%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Cannot install PyQt5 The Next CEO of Stack OverflowCannot install tcpreplay 3.4.4cannot...

Kapp-Putsch Acontecimentos | Outros artigos | Menu de navegação

Why did early computer designers eschew integers? The Next CEO of Stack OverflowWhat register...