The difference between single quotes, double quotes and “ in shell
name="hello" echo name echo "$name" echo '$name' echo $name
Guess what the output is, actually it is
In the shell, variables under double quotes can be parsed, but variables under single quotes cannot.
In addition, the results returned by “ wrapped commands can also be stored in variables
list=`date` echo $list
The difference between source xx.sh
, bash xx.sh
, ./xx.sh
in shell
All three commands involve executing a script file in the shell, but there are some differences between them. Let me explain it to you:
source xx.sh
Used to run the specified script file (usually a Shell script with a .sh extension) in the current Shell environment. After execution, the commands in the script will be executed directly in the current Shell process without creating a new child process. This means that variable assignments, function definitions, etc. in the script are visible and affect the current Shell environment.
For example:
source myscript.sh
bash xx.sh
This command is used to execute the specified script file in a new bash subprocess. It creates a new bash shell environment and then executes the script in that environment. In this case, variable assignments and other operations in the script will only take effect in the environment of the child process and have no impact on the parent Shell environment in which the command is called.
For example:
bash myscript.sh
./xx.sh
This command is used to run the executable script file in the current directory. You need to make sure the script file has execute permissions (use chmod + x xx.sh to add execute permissions). This method will also create a new child process to run the script, and similar to using bash xx.sh, variable assignments and other operations in the script will only take effect in the child process.
For example:
./myscript.sh
To sum up, the method you choose depends on the environment in which you want to run the script and whether the operations in the script need to affect the current shell environment. If you want to modify the current Shell environment, you usually use the source or . command, and if you just need to execute the script without affecting the current environment, you can use the bash or ./ command.
Environment variables
-
Set temporary environment variables:
In the current shell session, you can use the
export
command to set temporary environment variables. Suppose you wish to set a temporary variableMY_TEMP_VAR
toHello, World!
:export MY_TEMP_VAR="Hello, World!" echo $MY_TEMP_VAR
This will print
Hello, World!
in the current session. -
Permanently set user-level environment variables:
Suppose you want to set a permanent environment variable that takes effect for all sessions of a specific user. You can add variables to the user’s
~/.bashrc
file. Open the file and add the following content:export MY_USER_VAR="This is my user-level variable"
Then use the following command to make the changes effective:
source ~/.bashrc
Now,
$MY_USER_VAR
will be available in every session when you log in. -
Permanently set global environment variables:
Suppose you want to set an environment variable that is system-wide and affects all users and sessions. You can create a new file, such as
/etc/profile.d/my_global_vars.sh
, and add the following content to it:export GLOBAL_VAR="This is a global variable"
You do not need to run the
source
command manually because the files in/etc/profile.d/
will be loaded automatically when the system starts. -
Check environment variables:
Use the
set
,env
anddeclare
commands to check environment variables. For example:# Display all variables set # Show global variables env #Similar to set, displays all variables declare
These commands allow you to view various variables set in the current session.
-
Use the
export
command to display and set environment variable values:Use the
export
command to display and set the values of environment variables. For example:# Display all exported environment variables export # Set new environment variables export NEW_VAR="New variable value" # Update existing environment variables export MY_TEMP_VAR="Updated value"
These commands allow you to view and manipulate environment variables in the current session.
Revoke environment variables
To undo an environment variable, you can use the unset
command. This command allows you to delete an existing environment variable from the current shell session. Please note that using the unset
command will only delete variables in the current session and will not affect other sessions or persistence settings.
Here is an example of how to use the unset
command to undo an environment variable:
-
Revoke temporary environment variables:
If you have previously set a temporary environment variable using the
export
command, you can use theunset
command to delete it. For example, suppose you set a temporary variableMY_TEMP_VAR
and now want to delete it:export MY_TEMP_VAR="Hello, World!" echo $MY_TEMP_VAR # Output: Hello, World! unsetMY_TEMP_VAR echo $MY_TEMP_VAR # Output: empty
In this example, the
unset
command deletes the previously set temporary variable. -
Permanently delete user-level environment variables:
To permanently remove user-level environment variables, you need to edit the user’s configuration file (such as
~/.bashrc
) and delete the corresponding lines. For example, suppose you set a variableMY_USER_VAR
in~/.bashrc
:export MY_USER_VAR="This is my user-level variable"
You can undo the setting of a variable by editing the file and deleting the line, then using the
source
command to make the changes effective. -
Delete global environment variables:
To remove global environment variables, you can edit the global configuration file (such as
/etc/profile.d/my_global_vars.sh
) and delete the corresponding setting lines. After deleting the line, the global variable will no longer take effect on system startup.
Read-only variable
A read-only variable in the Shell is a special type of variable whose value cannot be modified once it is set. This can be used to ensure that certain important configuration information or environment variables are not accidentally changed. Read-only variables are useful in scripting and system administration to help avoid potential errors and security issues.
In most shells (such as Bash), you can use the readonly
keyword to define read-only variables. Here is some important information about read-only variables:
-
Define read-only variables:
A variable can be declared read-only using the
readonly
keyword. Once a variable is set to read-only, any attempt to modify its value will be rejected.readonly MY_READONLY_VAR="This variable is read-only"
-
Try to modify a read-only variable:
If you try to modify a read-only variable, the shell will complain and the modification will not be allowed.
MY_READONLY_VAR="Trying to change the value" # An error will be reported here
-
Application of read-only variables:
Read-only variables are useful in the following situations:
-
Protect configuration information: You can define configuration information used in scripts as read-only variables to ensure that the script does not accidentally modify the configuration at runtime.
-
Environment variable protection: Some important environment variables, such as
PATH
, can be declared read-only to prevent them from being maliciously modified, thereby affecting the normal operation of the system. -
Script parameters: If your script needs to receive parameters, you can define these parameters as read-only variables to prevent accidental changes within the script.
-
-
Cancel read-only setting:
To unset a variable as read-only, you can use the
unset
command to unset the variable from read-only status.readonly MY_READONLY_VAR unset MY_READONLY_VAR # This will cancel the read-only setting
Shell configuration loading sequence
Read-only variables can play a great role in writing safe and stable scripts. By declaring critical information as read-only, you reduce potential errors and security vulnerabilities, ensuring script and system reliability.
In Linux and Unix-like systems, the Shell configuration file will be loaded in a certain order at startup to set initial environment variables, aliases, functions, etc. Different shells have different configuration files and loading orders. Here are some common shell configuration files and their loading order:
-
Bash Shell:
/etc/profile
: System-level configuration file, valid for all users./etc/bash.bashrc
: System-level configuration file used to define global aliases and functions.~/.bash_profile
: User personal configuration file, used for commands executed when the user logs in.~/.bashrc
: User personal configuration file, used to define user-specific aliases, functions and settings./etc/profile.d/
Directory: Usually, system administrators can place specific configuration files in this directory, and these files will be loaded when the user logs in.
Loading order:
/etc/profile
/etc/bash.bashrc
~/.bash_profile
or~/.bash_login
or~/.profile
(choose one based on existence)~/.bashrc
-
Zsh Shell:
/etc/zsh/zshenv
: System-level configuration file, loaded before all users.~/.zshenv
: User personal configuration file, used to define user-specific environment variables./etc/zsh/zprofile
: System-level configuration file, used for commands executed when users log in.~/.zprofile
: User personal configuration file, used for commands executed when the user logs in./etc/zsh/zshrc
: System-level configuration file used to define global aliases and functions.~/.zshrc
: User personal configuration file, used to define user-specific aliases, functions and settings./etc/zsh/zlogin
: System-level configuration file, executed when the user logs in.~/.zlogin
: User personal configuration file, executed when the user logs in.~/.zlogout
: User personal configuration file, executed when the user logs out.
Loading order:
/etc/zsh/zshenv
~/.zshenv
/etc/zsh/zprofile
~/.zprofile
/etc/zsh/zshrc
~/.zshrc
/etc/zsh/zlogin
~/.zlogin
~/.zlogout
The order in which configurations are loaded may vary slightly from shell to shell, but the general idea is to load system-level and user-level configuration files at startup to set the appropriate environment variables, aliases, and functions to ensure that the shell session runs the expected way.
Special variables
In Shell programming, there are some predefined variables called Special Variables, which provide important information about the script and the Shell environment. These special variables may vary in different shells. Here are some common special variables and their meanings:
-
$0
: Indicates the name of the current script or command. Usually used to get the name of the script itself for logging or error information. -
$1
,$2
, …: Represents the parameters of a script or function.$1
represents the first parameter,$2
represents the second parameter, and so on. -
$#
: Indicates the number of parameters passed to the script or function. -
$@
: Indicates all parameters passed to the script or function. Each parameter is treated as an independent parameter. -
$*
: Similar to$@
, representing all arguments passed to a script or function, but treating them as a single string. -
$?
: Indicates the exit status code (return value) of the previous command. Usually used to check whether the previous command was executed successfully (exit status code is 0). -
$$
: Indicates the process ID (PID) of the current script or command. -
$!
: Indicates the PID of the last process running in the background. -
$USER
(in Bash): Represents the username of the current user. -
$HOME
(in Bash): Represents the current user’s home directory path. -
$SHELL
(in Bash): Indicates the path of the Shell used by the current user. -
$PWD
(in Bash): Represents the path of the current working directory. -
$OLDPWD
(in Bash): Represents the path of the previous working directory. -
$IFS
(in Bash): Represents the internal field delimiter, used to define fields in strings separated by delimiters such as spaces and tabs. -
$_
: represents the last parameter of the previous command. In an interactive shell session, it is often used to obtain the parameters of the previous command for use in subsequent commands.
In shell programming, both $*
and $@
represent all parameters passed to a script or function, but there are some subtle differences in how parameters are handled.
-
$*
: Treat all parameters as a single string. Parameters are separated by an IFS (Internal Field Separator) represented by the value of the first character. Normally, the default IFS is spaces. This means that$*
will combine all parameters into a single string. -
$@
: Treat each parameter as a separate item, each parameter retains its original form and is not merged into a single string. This makes$@
more flexible when handling parameters and is not affected by IFS.
Here is an example showing the difference between $*
and $@
when handling parameters:
#!/bin/bash echo "Using \$*:" for arg in $*; do echo $arg done echo -e "\\ Using \$@:" for arg in $@; do echo $arg done
Suppose you save the above code as args_example.sh
, and then run the following command:
bash args_example.sh arg1 arg2 "arg3 with spaces"
The output will be:
Using $*: arg1 arg2 arg3 with spaces Using $@: arg1 arg2 arg3 with spaces
As you can see, in $*
, the parameters are separated by spaces, and “arg3 with spaces” is split into multiple parts. In $@
, the parameters retain their original form and are not affected by spaces. This is the difference between them. In general, it is recommended to use $@
in most cases, as it is more consistent with how parameters are generally expected to be handled.
$_
: represents the last parameter of the previous command. In an interactive shell session, it is often used to obtain the parameters of the previous command for use in subsequent commands.
For example, assume you execute the following command in the shell:
$ echo "Hello, World!"
You can then use $_
to reference the last argument of the previous command, like this:
$ echo "The last command was: $_" The last command was: Hello, World!
In this example, $_
will be replaced with the parameter of the previous command, which is “Hello, World!”.
Note that $_
only contains the last parameter of the previous command, not the entire parameter list. If you want to get the entire parameter list, you should use $@
or $*
.
Some basic built-in commands of bash
Here is a brief description of some basic bash built-in commands:
-
echo
: used to output text or the value of a variable in the terminal. -
eval
: used to execute parameters as commands and return the results. -
exec
: Used to replace the current process and execute the specified command. Once theexec
command is executed, the original process will be replaced and will not return to the original script. -
export
: Used to export variables as environment variables, making them visible in the current shell session and its subprocesses. -
read
: Used to read user input from standard input and store it in variables. -
shift
: used to move positional parameters (command line parameters) to the left, making$2
become$1
,$3
becomes$2
, and so on. This is useful when handling multiple command line arguments.
echo in action
The echo
command is used to output text or variable values to the terminal. Here are some commonly used echo
command options:
-
-n
: output without line breaks. By default,echo
will automatically wrap text after outputting it. Use the-n
option to disable line wrapping and keep the output on the same line. -
-e
: Parse special symbols in strings. Use the-e
option to allowecho
to parse special characters in the string. For example,\\
represents a newline character, and
\t
represents tab characters, etc. This inserts special characters into the output. -
\
: represents escape characters. Use backslash\
in a string to escape special characters, for example,"
represents a double quote,\
Represents the backslash itself.
For example, here are some examples:
echo "Hello, World!" # Output: Hello, World! echo -n "Hello, " # Output: Hello, echo "World!" # Output: World! echo -e "Line 1\\ Line 2\tTabbed" # Output: # Line 1 # Line 2 Tabbed echo "This is a quote: "To be or not to be"" # Output: This is a quote: "To be or not to be"
read actual combat
The read
command is used to read user input from standard input (usually the terminal) and store the input content in one or more variables. The following is the basic usage of the read
command:
read [options] variable1 variable2 ...
-
options
: You can use some options to control the behavior of theread
command, such as setting the timeout, specifying the delimiter, etc. Commonly used options include:-
-p
: Display a prompt to prompt the user for input. -
-s
: Silent mode, the content entered by the user will not be displayed on the terminal.
-
-
variable1 variable2 ...
: Specify one or more variables to store user input. The input content will be separated by spaces or specified delimiters and stored in each variable in turn.
Here is a simple example that demonstrates how to use the read
command to read user input and store it in a variable:
read -p "Please enter your name: " name echo "Hello, $name! Welcome to this system."
When running the above code, the user will be prompted to enter a name, the content entered by the user will be stored in the name
variable, and a welcome message will be output through the echo
command.
-s is generally used to enter passwords
read -s -p "Please enter password: " password echo "The password you entered is: $password"
The above code will continue to prompt the user for numbers until the user enters the letter “q”.
eval in action
command="ls ../ -a" eval $command
exec practice
exec date
After executing this command, exit will be executed immediately to exit the current user.