Bash script variable placement
Clash 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 hello
as result.
If I run:
#!/bin/bash
USERR=John
echo "hello $USERR"
then i get hello John
as result.
bash
add a comment |Â
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 hello
as result.
If I run:
#!/bin/bash
USERR=John
echo "hello $USERR"
then i get hello John
as result.
bash
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
add a comment |Â
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 hello
as result.
If I run:
#!/bin/bash
USERR=John
echo "hello $USERR"
then i get hello John
as result.
bash
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 hello
as result.
If I run:
#!/bin/bash
USERR=John
echo "hello $USERR"
then i get hello John
as result.
bash
bash
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
add a comment |Â
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
add a comment |Â
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.
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"
prints1
because$x
is substituted beforex=2 echo "$x"
is run.
â BallpointBen
Sep 4 at 17:26
add a comment |Â
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:
- brace expansion
- tilde expansion
- parameter and variable expansion
- arithmetic expansion
- command substitution (done in a left-to-right fashion)
- word splitting
- pathname expansion
- quote removal
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"
printssome 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
 |Â
show 1 more comment
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.
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"
prints1
because$x
is substituted beforex=2 echo "$x"
is run.
â BallpointBen
Sep 4 at 17:26
add a comment |Â
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.
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"
prints1
because$x
is substituted beforex=2 echo "$x"
is run.
â BallpointBen
Sep 4 at 17:26
add a comment |Â
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.
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.
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"
prints1
because$x
is substituted beforex=2 echo "$x"
is run.
â BallpointBen
Sep 4 at 17:26
add a comment |Â
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"
prints1
because$x
is substituted beforex=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
add a comment |Â
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:
- brace expansion
- tilde expansion
- parameter and variable expansion
- arithmetic expansion
- command substitution (done in a left-to-right fashion)
- word splitting
- pathname expansion
- quote removal
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"
printssome 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
 |Â
show 1 more comment
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:
- brace expansion
- tilde expansion
- parameter and variable expansion
- arithmetic expansion
- command substitution (done in a left-to-right fashion)
- word splitting
- pathname expansion
- quote removal
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"
printssome 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
 |Â
show 1 more comment
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:
- brace expansion
- tilde expansion
- parameter and variable expansion
- arithmetic expansion
- command substitution (done in a left-to-right fashion)
- word splitting
- pathname expansion
- quote removal
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:
- brace expansion
- tilde expansion
- parameter and variable expansion
- arithmetic expansion
- command substitution (done in a left-to-right fashion)
- word splitting
- pathname expansion
- quote removal
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"
printssome 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
 |Â
show 1 more comment
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"
printssome 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
 |Â
show 1 more comment
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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