Skip to content
Vidar Holen edited this page Dec 31, 2020 · 2 revisions

To assign positional parameters, use 'set -- first second ..' (or use [ ] to compare).

Problematic code:

if [ -z "$1" ]
then
  $1="help"
fi

or

if $1="help"
then
  echo "Usage: $0 filename"
fi

Correct code:

if [ -z "$1" ]
then
  set -- "help"
fi

or

if [ $1 = "help" ]
then
  echo "Usage: $0 filename"
fi

Rationale:

You have a command on the form $2=value.

If the goal is to assign a new value to the positional parameters, use the set builtin: set -- one two .. will cause $1 to be "one" and $2 to be "two".

If you instead want to compare the value, use [ ] and add spaces: [ "$1" = "foo" ]

Exceptions:

None

Related resources:

  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!

ShellCheck

Each individual ShellCheck warning has its own wiki page like SC1000. Use GitHub Wiki's "Pages" feature above to find a specific one, or see Checks.

Clone this wiki locally