Versions Compared

Key

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

...

  • 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.

...

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

Example:

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

...

Importantly, if an argument to be passed itself contains spaces, the argument must be enclosed in single or double quotes (or, more ugly, the spaces can be backspace-quotedescaped, e.g. "\ ")

Code Block
languagebash
# Call a custom script passing 2 arguments
my_script.sh arg1 "arg2 has embedded spaces"

# Call a function passing 2 arguments
my_function arg1 'arg2 has embedded spaces'

...

Code Block
languagebash
function myfn() { echo "arg 1: $1"; echo "arg 2: $2"; echo "all args: $@"; }
myfn foo bar baz wonk
myfn foo "bar baz" wonk
myfn "foo bar" baz wonk

As described in Intro Unix: Quoting in the shell, this is the main function of both single and double quotes – to group text containing whitespace into one item.

...

Script arguments will initially be:

  • $1 - helloWorld
  • $2 - My
  • $3 - name
  • $4 - is
  • $5 - Anna
  • $@ - helloWorld My name is Anna

Command line processing captures the 1st script argument then pops it off with shift.

...

After shift is called, script arguments are:

  • $1 - My My
  • $2 - name name
  • $3 - is is
  • $4 - Anna Anna
  • $@ - My name is Anna

The CMD variable matches the helloWorld) clause of the case/esac statement, so arguments are passed to the helloWorld function in the "$@" built-in variable.

...

The helloWorld function captures the first two arguments into local variables txt1 and txt2, then shifts them off, storing the remaining function arguments ($@) in the rest variable. It then echos the variable values, surrounded by single quotes:

...

  • $1My → stored in local variable txt1
  • $2name → stored in local variable txt2
  • $3is
  • $4Anna
  • $@ - My name is Anna

...

  • My → now stored in local variable txt1
  • name → now stored in local variable txt2
  • $1 - is
  • $2 - Anna
  • $@ - is Anna → stored in local variable rest

excercise 1

How would you call the helloWorld command to produce this output?

...

Expand
titleSolution

Here single quotes or backslash must be used so that the shell does not try to evaluate "$foo$foo" and "\" before passing the arguments into the script.

Code Block
languagebash
~/workshop/step_01.sh helloWorld 'to evaluate a variable, use the dollar sign, e.g. $foo' 'use backslash (\) to escape special characters'

# or

~/workshop/step_01.sh helloWorld 'to evaluate a variable, use the dollar sign, e.g. $foo' "use backslash (\\) to escape special characters"


...