Bash script variable placement

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
8
down vote

favorite












I started to learn Bash scripting and I'm using Bash scripting tutorial



There it says




Before Bash interprets (or runs) every line of our script it first checks to see if any variable names are present. For every variable it has identified, it replaces the variable name with its value. Then it runs that line of code and begins the process again on the next line.




So does Bash first run through the whole script to find variables? I'm not sure whether this is what the author tried to say but if yes I guess it is not correct?



when I execute:



#!/bin/bash


echo "hello $USERR"



USERR=John


I get helloas result.



If I run:



#!/bin/bash


USERR=John

echo "hello $USERR"


then i get hello John as result.










share|improve this question

















  • 5




    I would disregard a tutorial whose first code sample does the most elementary mistake of not quoting parameter expansions (and forgetting the --, and not checking for failure of commands...). Security implications of forgetting to quote a variable in bash/POSIX shells
    – Stéphane Chazelas
    Sep 4 at 8:45















up vote
8
down vote

favorite












I started to learn Bash scripting and I'm using Bash scripting tutorial



There it says




Before Bash interprets (or runs) every line of our script it first checks to see if any variable names are present. For every variable it has identified, it replaces the variable name with its value. Then it runs that line of code and begins the process again on the next line.




So does Bash first run through the whole script to find variables? I'm not sure whether this is what the author tried to say but if yes I guess it is not correct?



when I execute:



#!/bin/bash


echo "hello $USERR"



USERR=John


I get helloas result.



If I run:



#!/bin/bash


USERR=John

echo "hello $USERR"


then i get hello John as result.










share|improve this question

















  • 5




    I would disregard a tutorial whose first code sample does the most elementary mistake of not quoting parameter expansions (and forgetting the --, and not checking for failure of commands...). Security implications of forgetting to quote a variable in bash/POSIX shells
    – Stéphane Chazelas
    Sep 4 at 8:45













up vote
8
down vote

favorite









up vote
8
down vote

favorite











I started to learn Bash scripting and I'm using Bash scripting tutorial



There it says




Before Bash interprets (or runs) every line of our script it first checks to see if any variable names are present. For every variable it has identified, it replaces the variable name with its value. Then it runs that line of code and begins the process again on the next line.




So does Bash first run through the whole script to find variables? I'm not sure whether this is what the author tried to say but if yes I guess it is not correct?



when I execute:



#!/bin/bash


echo "hello $USERR"



USERR=John


I get helloas result.



If I run:



#!/bin/bash


USERR=John

echo "hello $USERR"


then i get hello John as result.










share|improve this question













I started to learn Bash scripting and I'm using Bash scripting tutorial



There it says




Before Bash interprets (or runs) every line of our script it first checks to see if any variable names are present. For every variable it has identified, it replaces the variable name with its value. Then it runs that line of code and begins the process again on the next line.




So does Bash first run through the whole script to find variables? I'm not sure whether this is what the author tried to say but if yes I guess it is not correct?



when I execute:



#!/bin/bash


echo "hello $USERR"



USERR=John


I get helloas result.



If I run:



#!/bin/bash


USERR=John

echo "hello $USERR"


then i get hello John as result.







bash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 4 at 7:41









blablatrace

565




565







  • 5




    I would disregard a tutorial whose first code sample does the most elementary mistake of not quoting parameter expansions (and forgetting the --, and not checking for failure of commands...). Security implications of forgetting to quote a variable in bash/POSIX shells
    – Stéphane Chazelas
    Sep 4 at 8:45













  • 5




    I would disregard a tutorial whose first code sample does the most elementary mistake of not quoting parameter expansions (and forgetting the --, and not checking for failure of commands...). Security implications of forgetting to quote a variable in bash/POSIX shells
    – Stéphane Chazelas
    Sep 4 at 8:45








5




5




I would disregard a tutorial whose first code sample does the most elementary mistake of not quoting parameter expansions (and forgetting the --, and not checking for failure of commands...). Security implications of forgetting to quote a variable in bash/POSIX shells
– Stéphane Chazelas
Sep 4 at 8:45





I would disregard a tutorial whose first code sample does the most elementary mistake of not quoting parameter expansions (and forgetting the --, and not checking for failure of commands...). Security implications of forgetting to quote a variable in bash/POSIX shells
– Stéphane Chazelas
Sep 4 at 8:45











2 Answers
2






active

oldest

votes

















up vote
10
down vote



accepted











So does Bash first run through the whole script to find variables?




Nope. As you yourself discovered in your example, Bash scripts are executed from top to bottom.



A good practice is to define all variables that you need at the top of your script.






share|improve this answer






















  • Thank you to both of you for clarifying my doubts!
    – blablatrace
    Sep 4 at 8:00










  • Thanks for the edit @Mefitico
    – maulinglawns
    Sep 4 at 14:30










  • However, Bash does first run through lines to find variables. x=1; x=2 echo "$x" prints 1 because $x is substituted before x=2 echo "$x" is run.
    – BallpointBen
    Sep 4 at 17:26

















up vote
8
down vote













That's a very sloppy way of saying that the shell scans for expansions, such as for example variables (but also command substitutions etc.).



The text could be interpreted to imply that the shell reads the whole script and looks for variables on every line before execution. That is not so. It processes the script command by command (a command could span several lines).



The bash shell does the following with each command before executing it:



  1. brace expansion

  2. tilde expansion

  3. parameter and variable expansion

  4. arithmetic expansion

  5. command substitution (done in a left-to-right fashion)

  6. word splitting

  7. pathname expansion

  8. quote removal





share|improve this answer






















  • yes, this is how I interpreted the line. I'll go through the material anyway but are there any books, web sites worth looking at ? Thank you in advance!
    – blablatrace
    Sep 4 at 8:12






  • 3




    @blablatrace I'd recommend unix.stackexchange.com as a good resource for learning how to use and program in the shell, even though the quality is somewhat variable at times. The community is generally friendly too. Check out the chat too (a bit empty in there sometimes though).
    – Kusalananda
    Sep 4 at 8:43











  • Maybe you should be more clear in what you mean by 'processes redirections' -- I understand what's about (that eg. 'a=">"; echo yes $a foo' won't write 'yes' to 'foo'); but redirections are certainly processed AFTER expansions, eg. 'a="a b c"; echo >$a' is an error, which shows the shell tries to make sense of the redirection after it does variable expansion. And variable assignments are also special in the same manner as redirections -- 'q=a; $q=12 cmd' will try to run a command named 'a=12', not 'cmd' with a var a = 12 in its environment.
    – mosvy
    Sep 4 at 16:33











  • Also, if there are multiple simple commands on the same line (separated by ;, || or &&), they're expanded and executed one at a time. Thus, foo="some value"; echo "$foo" prints some value because the reference to $foo doesn't get expanded until after the assignment command has run. BTW, commands can also be separated by &, but that's a bit of a special case; with that, the first command gets run in the background so the second command doesn't wait for it to finish.
    – Gordon Davisson
    Sep 4 at 16:39






  • 2




    Other good bash scripting resources: Greg's wiki and BashFAQ, the Bash Hacker's Wiki, and shellcheck.net to check your scripts for common problems.
    – Gordon Davisson
    Sep 4 at 16:44










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f466711%2fbash-script-variable-placement%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
10
down vote



accepted











So does Bash first run through the whole script to find variables?




Nope. As you yourself discovered in your example, Bash scripts are executed from top to bottom.



A good practice is to define all variables that you need at the top of your script.






share|improve this answer






















  • Thank you to both of you for clarifying my doubts!
    – blablatrace
    Sep 4 at 8:00










  • Thanks for the edit @Mefitico
    – maulinglawns
    Sep 4 at 14:30










  • However, Bash does first run through lines to find variables. x=1; x=2 echo "$x" prints 1 because $x is substituted before x=2 echo "$x" is run.
    – BallpointBen
    Sep 4 at 17:26














up vote
10
down vote



accepted











So does Bash first run through the whole script to find variables?




Nope. As you yourself discovered in your example, Bash scripts are executed from top to bottom.



A good practice is to define all variables that you need at the top of your script.






share|improve this answer






















  • Thank you to both of you for clarifying my doubts!
    – blablatrace
    Sep 4 at 8:00










  • Thanks for the edit @Mefitico
    – maulinglawns
    Sep 4 at 14:30










  • However, Bash does first run through lines to find variables. x=1; x=2 echo "$x" prints 1 because $x is substituted before x=2 echo "$x" is run.
    – BallpointBen
    Sep 4 at 17:26












up vote
10
down vote



accepted







up vote
10
down vote



accepted







So does Bash first run through the whole script to find variables?




Nope. As you yourself discovered in your example, Bash scripts are executed from top to bottom.



A good practice is to define all variables that you need at the top of your script.






share|improve this answer















So does Bash first run through the whole script to find variables?




Nope. As you yourself discovered in your example, Bash scripts are executed from top to bottom.



A good practice is to define all variables that you need at the top of your script.







share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 4 at 13:33









Mefitico

1034




1034










answered Sep 4 at 7:47









maulinglawns

6,06821024




6,06821024











  • Thank you to both of you for clarifying my doubts!
    – blablatrace
    Sep 4 at 8:00










  • Thanks for the edit @Mefitico
    – maulinglawns
    Sep 4 at 14:30










  • However, Bash does first run through lines to find variables. x=1; x=2 echo "$x" prints 1 because $x is substituted before x=2 echo "$x" is run.
    – BallpointBen
    Sep 4 at 17:26
















  • Thank you to both of you for clarifying my doubts!
    – blablatrace
    Sep 4 at 8:00










  • Thanks for the edit @Mefitico
    – maulinglawns
    Sep 4 at 14:30










  • However, Bash does first run through lines to find variables. x=1; x=2 echo "$x" prints 1 because $x is substituted before x=2 echo "$x" is run.
    – BallpointBen
    Sep 4 at 17:26















Thank you to both of you for clarifying my doubts!
– blablatrace
Sep 4 at 8:00




Thank you to both of you for clarifying my doubts!
– blablatrace
Sep 4 at 8:00












Thanks for the edit @Mefitico
– maulinglawns
Sep 4 at 14:30




Thanks for the edit @Mefitico
– maulinglawns
Sep 4 at 14:30












However, Bash does first run through lines to find variables. x=1; x=2 echo "$x" prints 1 because $x is substituted before x=2 echo "$x" is run.
– BallpointBen
Sep 4 at 17:26




However, Bash does first run through lines to find variables. x=1; x=2 echo "$x" prints 1 because $x is substituted before x=2 echo "$x" is run.
– BallpointBen
Sep 4 at 17:26












up vote
8
down vote













That's a very sloppy way of saying that the shell scans for expansions, such as for example variables (but also command substitutions etc.).



The text could be interpreted to imply that the shell reads the whole script and looks for variables on every line before execution. That is not so. It processes the script command by command (a command could span several lines).



The bash shell does the following with each command before executing it:



  1. brace expansion

  2. tilde expansion

  3. parameter and variable expansion

  4. arithmetic expansion

  5. command substitution (done in a left-to-right fashion)

  6. word splitting

  7. pathname expansion

  8. quote removal





share|improve this answer






















  • yes, this is how I interpreted the line. I'll go through the material anyway but are there any books, web sites worth looking at ? Thank you in advance!
    – blablatrace
    Sep 4 at 8:12






  • 3




    @blablatrace I'd recommend unix.stackexchange.com as a good resource for learning how to use and program in the shell, even though the quality is somewhat variable at times. The community is generally friendly too. Check out the chat too (a bit empty in there sometimes though).
    – Kusalananda
    Sep 4 at 8:43











  • Maybe you should be more clear in what you mean by 'processes redirections' -- I understand what's about (that eg. 'a=">"; echo yes $a foo' won't write 'yes' to 'foo'); but redirections are certainly processed AFTER expansions, eg. 'a="a b c"; echo >$a' is an error, which shows the shell tries to make sense of the redirection after it does variable expansion. And variable assignments are also special in the same manner as redirections -- 'q=a; $q=12 cmd' will try to run a command named 'a=12', not 'cmd' with a var a = 12 in its environment.
    – mosvy
    Sep 4 at 16:33











  • Also, if there are multiple simple commands on the same line (separated by ;, || or &&), they're expanded and executed one at a time. Thus, foo="some value"; echo "$foo" prints some value because the reference to $foo doesn't get expanded until after the assignment command has run. BTW, commands can also be separated by &, but that's a bit of a special case; with that, the first command gets run in the background so the second command doesn't wait for it to finish.
    – Gordon Davisson
    Sep 4 at 16:39






  • 2




    Other good bash scripting resources: Greg's wiki and BashFAQ, the Bash Hacker's Wiki, and shellcheck.net to check your scripts for common problems.
    – Gordon Davisson
    Sep 4 at 16:44














up vote
8
down vote













That's a very sloppy way of saying that the shell scans for expansions, such as for example variables (but also command substitutions etc.).



The text could be interpreted to imply that the shell reads the whole script and looks for variables on every line before execution. That is not so. It processes the script command by command (a command could span several lines).



The bash shell does the following with each command before executing it:



  1. brace expansion

  2. tilde expansion

  3. parameter and variable expansion

  4. arithmetic expansion

  5. command substitution (done in a left-to-right fashion)

  6. word splitting

  7. pathname expansion

  8. quote removal





share|improve this answer






















  • yes, this is how I interpreted the line. I'll go through the material anyway but are there any books, web sites worth looking at ? Thank you in advance!
    – blablatrace
    Sep 4 at 8:12






  • 3




    @blablatrace I'd recommend unix.stackexchange.com as a good resource for learning how to use and program in the shell, even though the quality is somewhat variable at times. The community is generally friendly too. Check out the chat too (a bit empty in there sometimes though).
    – Kusalananda
    Sep 4 at 8:43











  • Maybe you should be more clear in what you mean by 'processes redirections' -- I understand what's about (that eg. 'a=">"; echo yes $a foo' won't write 'yes' to 'foo'); but redirections are certainly processed AFTER expansions, eg. 'a="a b c"; echo >$a' is an error, which shows the shell tries to make sense of the redirection after it does variable expansion. And variable assignments are also special in the same manner as redirections -- 'q=a; $q=12 cmd' will try to run a command named 'a=12', not 'cmd' with a var a = 12 in its environment.
    – mosvy
    Sep 4 at 16:33











  • Also, if there are multiple simple commands on the same line (separated by ;, || or &&), they're expanded and executed one at a time. Thus, foo="some value"; echo "$foo" prints some value because the reference to $foo doesn't get expanded until after the assignment command has run. BTW, commands can also be separated by &, but that's a bit of a special case; with that, the first command gets run in the background so the second command doesn't wait for it to finish.
    – Gordon Davisson
    Sep 4 at 16:39






  • 2




    Other good bash scripting resources: Greg's wiki and BashFAQ, the Bash Hacker's Wiki, and shellcheck.net to check your scripts for common problems.
    – Gordon Davisson
    Sep 4 at 16:44












up vote
8
down vote










up vote
8
down vote









That's a very sloppy way of saying that the shell scans for expansions, such as for example variables (but also command substitutions etc.).



The text could be interpreted to imply that the shell reads the whole script and looks for variables on every line before execution. That is not so. It processes the script command by command (a command could span several lines).



The bash shell does the following with each command before executing it:



  1. brace expansion

  2. tilde expansion

  3. parameter and variable expansion

  4. arithmetic expansion

  5. command substitution (done in a left-to-right fashion)

  6. word splitting

  7. pathname expansion

  8. quote removal





share|improve this answer














That's a very sloppy way of saying that the shell scans for expansions, such as for example variables (but also command substitutions etc.).



The text could be interpreted to imply that the shell reads the whole script and looks for variables on every line before execution. That is not so. It processes the script command by command (a command could span several lines).



The bash shell does the following with each command before executing it:



  1. brace expansion

  2. tilde expansion

  3. parameter and variable expansion

  4. arithmetic expansion

  5. command substitution (done in a left-to-right fashion)

  6. word splitting

  7. pathname expansion

  8. quote removal






share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 4 at 16:55

























answered Sep 4 at 8:06









Kusalananda

106k14209327




106k14209327











  • yes, this is how I interpreted the line. I'll go through the material anyway but are there any books, web sites worth looking at ? Thank you in advance!
    – blablatrace
    Sep 4 at 8:12






  • 3




    @blablatrace I'd recommend unix.stackexchange.com as a good resource for learning how to use and program in the shell, even though the quality is somewhat variable at times. The community is generally friendly too. Check out the chat too (a bit empty in there sometimes though).
    – Kusalananda
    Sep 4 at 8:43











  • Maybe you should be more clear in what you mean by 'processes redirections' -- I understand what's about (that eg. 'a=">"; echo yes $a foo' won't write 'yes' to 'foo'); but redirections are certainly processed AFTER expansions, eg. 'a="a b c"; echo >$a' is an error, which shows the shell tries to make sense of the redirection after it does variable expansion. And variable assignments are also special in the same manner as redirections -- 'q=a; $q=12 cmd' will try to run a command named 'a=12', not 'cmd' with a var a = 12 in its environment.
    – mosvy
    Sep 4 at 16:33











  • Also, if there are multiple simple commands on the same line (separated by ;, || or &&), they're expanded and executed one at a time. Thus, foo="some value"; echo "$foo" prints some value because the reference to $foo doesn't get expanded until after the assignment command has run. BTW, commands can also be separated by &, but that's a bit of a special case; with that, the first command gets run in the background so the second command doesn't wait for it to finish.
    – Gordon Davisson
    Sep 4 at 16:39






  • 2




    Other good bash scripting resources: Greg's wiki and BashFAQ, the Bash Hacker's Wiki, and shellcheck.net to check your scripts for common problems.
    – Gordon Davisson
    Sep 4 at 16:44
















  • yes, this is how I interpreted the line. I'll go through the material anyway but are there any books, web sites worth looking at ? Thank you in advance!
    – blablatrace
    Sep 4 at 8:12






  • 3




    @blablatrace I'd recommend unix.stackexchange.com as a good resource for learning how to use and program in the shell, even though the quality is somewhat variable at times. The community is generally friendly too. Check out the chat too (a bit empty in there sometimes though).
    – Kusalananda
    Sep 4 at 8:43











  • Maybe you should be more clear in what you mean by 'processes redirections' -- I understand what's about (that eg. 'a=">"; echo yes $a foo' won't write 'yes' to 'foo'); but redirections are certainly processed AFTER expansions, eg. 'a="a b c"; echo >$a' is an error, which shows the shell tries to make sense of the redirection after it does variable expansion. And variable assignments are also special in the same manner as redirections -- 'q=a; $q=12 cmd' will try to run a command named 'a=12', not 'cmd' with a var a = 12 in its environment.
    – mosvy
    Sep 4 at 16:33











  • Also, if there are multiple simple commands on the same line (separated by ;, || or &&), they're expanded and executed one at a time. Thus, foo="some value"; echo "$foo" prints some value because the reference to $foo doesn't get expanded until after the assignment command has run. BTW, commands can also be separated by &, but that's a bit of a special case; with that, the first command gets run in the background so the second command doesn't wait for it to finish.
    – Gordon Davisson
    Sep 4 at 16:39






  • 2




    Other good bash scripting resources: Greg's wiki and BashFAQ, the Bash Hacker's Wiki, and shellcheck.net to check your scripts for common problems.
    – Gordon Davisson
    Sep 4 at 16:44















yes, this is how I interpreted the line. I'll go through the material anyway but are there any books, web sites worth looking at ? Thank you in advance!
– blablatrace
Sep 4 at 8:12




yes, this is how I interpreted the line. I'll go through the material anyway but are there any books, web sites worth looking at ? Thank you in advance!
– blablatrace
Sep 4 at 8:12




3




3




@blablatrace I'd recommend unix.stackexchange.com as a good resource for learning how to use and program in the shell, even though the quality is somewhat variable at times. The community is generally friendly too. Check out the chat too (a bit empty in there sometimes though).
– Kusalananda
Sep 4 at 8:43





@blablatrace I'd recommend unix.stackexchange.com as a good resource for learning how to use and program in the shell, even though the quality is somewhat variable at times. The community is generally friendly too. Check out the chat too (a bit empty in there sometimes though).
– Kusalananda
Sep 4 at 8:43













Maybe you should be more clear in what you mean by 'processes redirections' -- I understand what's about (that eg. 'a=">"; echo yes $a foo' won't write 'yes' to 'foo'); but redirections are certainly processed AFTER expansions, eg. 'a="a b c"; echo >$a' is an error, which shows the shell tries to make sense of the redirection after it does variable expansion. And variable assignments are also special in the same manner as redirections -- 'q=a; $q=12 cmd' will try to run a command named 'a=12', not 'cmd' with a var a = 12 in its environment.
– mosvy
Sep 4 at 16:33





Maybe you should be more clear in what you mean by 'processes redirections' -- I understand what's about (that eg. 'a=">"; echo yes $a foo' won't write 'yes' to 'foo'); but redirections are certainly processed AFTER expansions, eg. 'a="a b c"; echo >$a' is an error, which shows the shell tries to make sense of the redirection after it does variable expansion. And variable assignments are also special in the same manner as redirections -- 'q=a; $q=12 cmd' will try to run a command named 'a=12', not 'cmd' with a var a = 12 in its environment.
– mosvy
Sep 4 at 16:33













Also, if there are multiple simple commands on the same line (separated by ;, || or &&), they're expanded and executed one at a time. Thus, foo="some value"; echo "$foo" prints some value because the reference to $foo doesn't get expanded until after the assignment command has run. BTW, commands can also be separated by &, but that's a bit of a special case; with that, the first command gets run in the background so the second command doesn't wait for it to finish.
– Gordon Davisson
Sep 4 at 16:39




Also, if there are multiple simple commands on the same line (separated by ;, || or &&), they're expanded and executed one at a time. Thus, foo="some value"; echo "$foo" prints some value because the reference to $foo doesn't get expanded until after the assignment command has run. BTW, commands can also be separated by &, but that's a bit of a special case; with that, the first command gets run in the background so the second command doesn't wait for it to finish.
– Gordon Davisson
Sep 4 at 16:39




2




2




Other good bash scripting resources: Greg's wiki and BashFAQ, the Bash Hacker's Wiki, and shellcheck.net to check your scripts for common problems.
– Gordon Davisson
Sep 4 at 16:44




Other good bash scripting resources: Greg's wiki and BashFAQ, the Bash Hacker's Wiki, and shellcheck.net to check your scripts for common problems.
– Gordon Davisson
Sep 4 at 16:44

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f466711%2fbash-script-variable-placement%23new-answer', 'question_page');

);

Post as a guest













































































這個網誌中的熱門文章

Is there any way to eliminate the singular point to solve this integral by hand or by approximations?

Why am i infinitely getting the same tweet with the Twitter Search API?

Carbon dioxide