Assigning a value in Bash
In Bash, if you try to assign a value to a variable using the following syntax:
and you receive a "Command not found" error, it means Bash is interpreting
VARNAME=value as a command and not a variable assignment.
The most common reason for this error is when there is a space before or after the equal sign.
For example, if you write:
VARNAME = value
Bash will try to execute the command "VARNAME" with argument "=". Since there is no command named "VARNAME", it will give you a "Command not found" error.
To fix this error, make sure there is no space before or after the equal sign:
or use the
export keyword to set the variable and export the value to the environment:
This will ensure that Bash interprets the statement as a variable assignment and not as a command.
Related Bash Tutorials
- Dynamic variable names in Bash
- How to parse command line arguments in Bash?
- Bash and shell script variable capitalization?
- How to get the directory where a Bash script is located from within the script itself?
- Parsing JSON with Unix tools
- How to set a variable to the output of a command in Bash?
- How to run Bash commands in Python?
- Extract filename and extension in Bash?
- Looping through the content of a file in Bash