Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • The basic structure of a command-processing script
  • Defining and evaluating bash variables
  • Grouping and evaluating values using double quotes ( " " ) or single quotes ( ' ' )
  • Functions in bash, and their local variables
  • Passing arguments to scripts and functions
  • Subtleties of quoting in bash

...

  • when referencing a positional argument variable with more than one digit (e.g. ${10})
  • to separate the variable evaluation from text immediately following (e.g. "${prefix}_file.txt")
    • since underscore characters ("_") are allowed in variable names, the braces are needed so that the shell does not think the variable name is prefix_file.

...

Code Block
languagebash
myvar="some text"
echo $myvar
echo ${myvar}
echo $myvar_more_text   # no output because the variable myvar_more_text is not defined
echo ${myvar}_more_text

When defining or evaluating environment variables there's also a difference between enclosing the value in double quotes ( "$foo" ) or single quotes ( '$foo' ) – see Quoting in the shell.

Example:

Code Block
languagebash
myvar="some text"
echo "$myvar"
echo '$myvar'

When

Functions

A bash function looks like this, with or without the function keyword.

...

Expand
titleSolution


Code Block
languagebash
# Empty quotes create an empty argument
~/workshop/step_01.sh helloWorld '' "My name is" Anna B


Quoting subtleties

You're probably already familiar with the We;ve already touched on difference kinds of Quoting in the shell. But there is an additional subtleties when handling script arguments.

...