DEV Community

Shubham Kumar Gupta
Shubham Kumar Gupta

Posted on

Bash Variables

Bash Variables

In the bash shell, variables are used to store values. Variables can be declared and assigned values using the following syntax:

variable_name=value
Enter fullscreen mode Exit fullscreen mode

For example:

name="Shubham Kumar Gupta"
Enter fullscreen mode Exit fullscreen mode

To reference the value stored in a variable, prefix the variable name with a '$' symbol.

echo "My name is $name"
Enter fullscreen mode Exit fullscreen mode
  • In bash, variables can store string, integer or floating-point values.
  • Variable names are case-sensitive.
  • Varibale names can only contain letters, numbers, and underscores and they must start with a letter or an underscore.

How to change $PATH variable

  • The $PATH variable in bash defines the search path for executables.
  • We can change it by modifying the contents of the $PATH environment variable.

Few ways to modify the $PATH variable in bash:

  • Temporarily modify $PATH for the current shell session:
PATH="$PATH:/new/directory"
Enter fullscreen mode Exit fullscreen mode
  • Permanently modify $PATH for the current user:
echo 'export PATH="$PATH:/new/directory"' >> ~/.bashrc
Enter fullscreen mode Exit fullscreen mode
  • Permanently modify $PATH for the system-wide shell:
echo 'export PATH="$PATH:/new/directory"' >> /etc/environment
Enter fullscreen mode Exit fullscreen mode
  • In each of the above examples, replace /new/directory with the path to the directory you want to add to the $PATH variable.
  • After modifying the $PATH variable, you may need to log out and log back in or restart your terminal session for the changes to take effect.

Top comments (0)