Skip to content

Latest commit

 

History

History
140 lines (91 loc) · 4.68 KB

01.Declaring_and_using_variables.md

File metadata and controls

140 lines (91 loc) · 4.68 KB

User Define Variables

  • A variable is a character string to which we assign a value.
  • A variable is nothing more than a pointer to the actual data. The shell enables you to create, assign, and delete variables.

Rules:

  • The name of a variable can contain only letters (a to z or A to Z), numbers ( 0 to 9) or the underscore character ( _ ).
  • Variable names cannot be reserved words
  • Variable names cannot have whitespace in between
  • The variable name cannot have special characters.
  • The first character of the variable name cannot be a number.
  • By convention, Unix shell variables will have their names in UPPERCASE.

The following examples are valid variable names −

_VARIABLE_NAME
VARIABLE_NAME
VARIABLE_1_NAME
vARIABLE_2_NAME

Following are the examples of invalid variable names −

2_VARNAME
-VARIABLENAME
VARIABLENAME-SOMENAME
SOMENAME_A!
  • The reason you cannot use other characters such as !, *, or - is that these characters have a special meaning for the shell.

Declaring and Using Variables in Linux

In Linux, you can declare variables without specifying their data types. Variables are case-sensitive, and conventionally, they are written in uppercase.

# Declare a variable
MY_VARIABLE="Hello, Linux!"

# Declare a numeric variable
NUM_VAR=42
  • Note that there must be no spaces around the "=" sign: VAR=value works; VAR = value doesn't work. In the first case, the shell sees the "=" symbol and treats the command as a variable assignment. In the second case, the shell assumes that VAR must be the name of a command and tries to execute it.

Variable Assignment

You can assign values to variables using the equal sign (=). No spaces are allowed around the equal sign.

# Assign a new value to an existing variable
MY_VARIABLE="New value"

Variable Usage

To use the value stored in a variable, precede the variable name with a dollar sign ($).

# Use the value of a variable
echo $MY_VARIABLE

Command Substitution

You can use command substitution to assign the output of a command to a variable.

# Command substitution
CURRENT_DATE=$(date)
echo "Today's date is: $CURRENT_DATE"

Special Variables

Linux has some special variables that provide information about the environment.

# Special variables
echo "Script name: $0"
echo "Number of arguments: $#"
echo "All arguments: $@"
echo "Exit status of the last command: $?"

Readonly Variables

You can make a variable read-only to prevent it from being changed later.

# Readonly variable
readonly READ_ONLY_VAR="This is a read-only variable"

System Variable in Shell Script

System variables in a Linux bash shell are created and maintained by the shell itself. These variables, with the exception of auto_resume and histchars, are defined in CAPITAL LETTERS. Configuration of the shell can be customized by modifying system variables such as PS1, PATH, LANG, HISTSIZE, and DISPLAY, among others.

There are several commands available for listing and setting environment variables in Linux:

  • env: Allows you to run another program in a custom environment without modifying the current one. When used without an argument, it prints a list of the current environment variables.
  • printenv: Prints all or the specified environment variables.
  • set: Sets or unsets shell variables. When used without an argument, it prints a list of all variables, including environment and shell variables, and shell functions.
  • unset: Deletes shell and environment variables.
  • export: Sets environment variables.

Common Environment Variables

  • USER: The current logged-in user.
  • HOME: The home directory of the current user.
  • EDITOR: The default file editor to be used. This is the editor that will be used when you type edit in your terminal.
  • SHELL: The path of the current user’s shell, such as bash or zsh.
  • LOGNAME: The name of the current user.
  • PATH: A list of directories to be searched when executing commands. When you run a command, the system will search those directories in this order and use the first found executable.
  • LANG: The current locales settings.
  • TERM: The current terminal emulation.

Conclusion

Variables are essential for storing and retrieving information in Linux scripts and commands. Understanding their declaration, assignment, and usage is crucial for effective scripting and automation.

For more detailed information, refer to the Bash documentation.