8

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.

1

2 Answers 2

11

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.

1
  • 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.CommentedSep 4, 2018 at 17:26
9

That's a very sloppy way of saying that the shell scans each command 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, not line by line. A command could span several lines. A command is not processed until the shell interpreter gets to it in its execution of the script.

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
6
  • 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!CommentedSep 4, 2018 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
    CommentedSep 4, 2018 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.
    – user313992
    CommentedSep 4, 2018 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.CommentedSep 4, 2018 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.CommentedSep 4, 2018 at 16:44

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.