Linux shell commands - variable usage

Table of contents

Variable definition:

Variable naming rules:

Use of variables:

Special variables:

Environment variables:

Delete variable:

shell variable example

Notes on using the shell


In Linux shell scripts, variables are symbolic names used to store and manipulate data. The following is a detailed description of variables in the Linux shell:

Variable definition:

A variable can be defined using:

variable_name=value

For example:

name="jojo"
age=25

Variable naming rules:

  •  Variable names consist of letters, numbers, and underscores, but cannot begin with a number.
  • Variable names are case sensitive.
  • It is recommended to use uppercase letters to define environment variables, and use lowercase letters to define ordinary variables. 

Use of variables:

  • When using a variable, you can refer to its value by preceding it with a dollar sign ($). For example:$variable_name
  • You can also use curly braces ({}) to delimit variable names. For example:${variable_name}
  • You can assign the value of a variable to another variable, or use it as an argument to a command.

Special variables:

  • $0: The file name of the current script.
  • $1, $2, ...: Command line parameters, indicating the first, second and other parameters.
  • $@: List of all command line arguments.
  • $#: The number of command line parameters.
  • $?: The exit status code of the previous command.
  • $$: Process ID of the current Shell process.

Environment variables:

  • Environment variables are global variables that can be accessed by all shell scripts and commands.
  • Ordinary variables can be exported as environment variables using  export the command.
  • Common environment variables include  PATH(specify the search path for executable files), HOME(the user's home directory), etc.

Delete variable:

  • unset A variable can be deleted using  the command. For example:unset variable_name

These are some basic concepts and usages about variables in Linux Shell. Variables can be used to store and manipulate data conveniently, improving the flexibility and reusability of scripts.

shell variable example

Here is an example of using shell commands to swap the values ​​of two variables:

#!/bin/bash

# 定义两个变量
var1="Hello"
var2="World"

echo "交换前的值:"
echo "var1 = $var1"
echo "var2 = $var2"

# 使用临时变量进行值交换
temp=$var1
var1=$var2
var2=$temp

echo "交换后的值:"
echo "var1 = $var1"
echo "var2 = $var2"

Save the above code as a script file (such as changeValue.sh), and then execute the script in the Shell. In the script, the values ​​of var1 and var2 are exchanged by using a temporary variable temp.

After executing the script, the output will show the variable values ​​before and after the swap.

Note: Use #!/bin/bash to declare the interpreter type in the script to ensure that the script can be executed correctly. In addition, in order to make the script file have execution permission, you can use the chmod +x changeValue.sh command to set it.

Notes on using the shell

There are a few things to consider when using shell variables:

  1. Variable naming: variable names should be descriptive, and try to avoid using the same names as shell built-in variables or system environment variables to avoid conflicts.

  2. Variable references: When using variables, use the dollar sign ($) for reference. If the variable name contains special characters, you can use curly braces ({}) to enclose the variable name to clarify the boundary of the variable.

    For example:

    # 直接引用变量
    echo $variable
    
    # 使用花括号明确变量边界
    echo ${variable}_suffix
  3. Assignment of variables: When assigning values ​​to variables, there must be no spaces on both sides of the equal sign (=). If the variable's value contains spaces or special characters, it should be enclosed in quotes.

    For example:

    # 赋值时不要有空格
    variable=value
    
    # 值包含空格,使用引号括起来
    variable="value with spaces"
  4. Variable scope: By default, variable scope is only valid in the current Shell process. If you need to use a variable in a subshell process, you can use exportthe command to export it as an environment variable.

  5. Variable deletion: You can use unsetcommands to delete a variable. After deletion, the variable no longer exists.

  6. Quotes around variables: When using variables, pay attention to the use of quotes. Single quotes (') will treat the variable as an ordinary string, while double quotes (") will retain the value of the variable and perform variable substitution.

    For example:

    variable="Hello"
    echo 'Value is $variable'  # 输出:Value is $variable
    echo "Value is $variable"  # 输出:Value is Hello
  7. Security of variables: Since Shell is an interpreted language, the value of variables can be input by the user, so pay attention to security when using variables. Avoid executing user input directly as commands to prevent security issues like command injection.

These considerations can help you use shell variables correctly and safely, and avoid some common mistakes and problems.

 

Guess you like

Origin blog.csdn.net/feng8403000/article/details/131613146