I'm learning bash scripting and found this on my /usr/share/bash-completion, line 305:

local cword words=()

What does it do? All tutorials online are just in the format

local var=value

Although I like answer given by jordanm I think it's equally important to show less experienced Linux users how to cope with such questions by themselves.

The suggested way is faster and more versatile than looking for answers at random pages showing up at Google search results page.

First, all commands that can be run in Bash without typing an explicit path to it such as ./command can be divided into two categories: Bash shell builtins and external commands. Bash shell builtins come installed with Bash and are part of it while external commands are not part of Bash. This is important because Bash shell builtins are documented inside man bash and their documentation can be also invoked with help command while external commands are usually documented in their own manpages or take some king of -h, --help flag. To check whether a command is a Bash shell builtin or an external command:

$ type local
local is a shell builtin

It will display how command would be interpreted if used as a command name (from help type). Here we can see that local is a shell builtin. Let's see another example:

$ type vim
vim is /usr/bin/vim

Here we can see that vim is not a shell builtin but an external command located in /usr/bin/vim. However, sometimes the same command could be installed both as an external command and be a shell builtin at the same time. Add -a to type to list all possibilities, for example:

$ type -a echo
echo is a shell builtin
echo is /usr/bin/echo
echo is /bin/echo

Here we can see that echo is both a shell builtin and an external command. However, if you just typed echo and pressed Return a shell builtin would be called because it appears first on this list. Note that all these versions of echo do not need to be the same. For example, on my system /usr/bin/echo takes --help flag while a builtin doesn't.

Ok, now when we know that local is a shell builtin let's find out how it works:

$ help local
local: local [option] name[=value] ...
Define local variables.

Create a local variable called NAME, and give it VALUE.  OPTION can
be any option accepted by `declare'.

Local variables can only be used within a function; they are visible
only to the function where they are defined and its children.

Exit Status:
Returns success unless an invalid option is supplied, an error occurs,
or the shell is not executing a function.

Note the first line: name[=value]. Everything between [ and ] is optional. It's a common convention used in many manpages and form of documentation in *nix world. That being said, command you asked about in your question is perfectly legal. In turn, ... character means that previous argument can be repeated. You can also read about this convention in some versions of man man:

The following conventions apply to the SYNOPSIS section and can be used
as a guide in other sections.

bold text          type exactly as shown.
italic text        replace with appropriate argument.
[-abc]             any or all arguments within [ ] are optional.
-a|-b              options delimited by | cannot be used together.
argument ...       argument is repeatable.
[expression] ...   entire expression within [ ] is repeatable.

So, at the end of the day, I hope that now you'll have an easier time understanding how different commands in Linux work.

  • 7
    Very nice answer. I was reading through it hoping you would address how a beginner could discover for themselves what the array assignment was, just from the code var=(), but I suppose that is a bit much to figure out without even knowing the name of what you're looking for. ;) – Wildcard Jan 6 '16 at 3:16
  • Do non-bash shells support local as well? – palswim Oct 13 '16 at 19:39
  • 3
    I signed in just so that I can upvote your answer :) – Harendra Singh Mar 15 '18 at 12:14
  • @Wildcard Since the array is bash syntax as opposed to a command, you can find it defined in man bash. Once in there, type /Arrays$ to jump to the section on arrays. (The trailing $ after Arrays is to prevent cycling through in-text references to the section.) From there you can type f to go forward a page or b to go backward. Type q to exit the man page when you're done. – Taylor Edmiston Mar 9 '19 at 12:41
  • Also, running help with no args will list all bash builtins if you're curious to see more. – Taylor Edmiston Mar 9 '19 at 12:46

The local keyword can take multiple variables. Providing the variable with a value is optional. Your example declares two variables, cword and words. The words variable is assigned an empty array.


local simply declares a variable to have scope only in the currently-defined function, so that the main executing environment cannot "see" the value. You can't use local outside a function. Example

func() {
   nonlocal="Non local variable"
   local onlyhere="Local variable"
echo $nonlocal 
echo $onlyhere

Output: Non local variable

So $onlyhere wasn't visible outside the scope of the function.

  • 1
    This answer makes more sense who are in need of a quick understanding. – Alex Raj Kaliamoorthy May 2 '19 at 13:17

It should be noted that Bash is dynamically scoped, which means local doesn't behave how you might expect from other languages.


#!/usr/bin/env bash


print1 () {
    echo "I am printing $x"

print2 () {
    local x=44

print1 # prints "I am printing 15"
print2 # prints "I am printing 44"
print1 # prints "I am printing 15"

So essentially this means local doesn't mean "local variable" it means "locally redefined global variable"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.