Linux Special Shell variables

Source: Internet
Author: User
Tags create directory

Special variables

Environment variables:

The system itself runs a class of variables that need to be created in advance by the Linux system

Mainly used for the user's working environment, including (user's host directory, command lookup path, user's current directory, login terminal, etc.) the value of environment variable is maintained by the operating system itself, change with the user's state change

Using env to tune the environment variables of the current system

[Email protected] ~]#EnvXdg_vtnr=1xdg_session_id=1Ssh_agent_pid=1925HOSTNAME=ZQ host name Imsettings_integrate_desktop=Yesgpg_agent_info=/run/user/ +/KEYRING/GPG:0:1vte_version=3803Xdg_menu_prefix=gnome-SHELL=/bin/Bashterm=xterm-256colorHISTSIZE= +Windowid=37748743Imsettings_module=Ibususer=zq Current User

Configuration file for environment variables in/etc/profile (global)

User Host Directory/home/zq/.bash profile (local)

$PATH

Define the default search path for a command

Echo $PATH/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/home/zq/.local/bin:/home/zq/bin

Note: Only commands executed by yourself are allowed to be used directly in the directory included in the path variable and can only be used with TAB completion if they are not in the path included, only through absolute paths or relative paths

echo Echo $USER  echo  $SHELL  Current script /bin/echo   $HOME   /root
Current language $LANG

Eg: put the script we wrote into the default search path for $path

[Email protected] ~]# first.SH cannot run this script directlyBash:first.SH: Command not found ... [[Email protected]~]#Echo$PATH because there is no such command in PATH/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/home/zq/.local/bin:/home/zq/Bin[[email protected]~]# path="$PATH:/root"Add this path to path."[[Email protected]~]#EchoThe path to this script is shown in $PATH path/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/home/zq/.local/bin:/home/zq/bin:/Root[[email protected]~]# first.SH can run this script directly.Before you need to pass./Execute themkdir: Unable to create directory"/root/shell": File already exists eno16777736:flags=4099<UP,BROADCAST,MULTICAST> MTU theinet192.168.1.63Netmask255.255.255.0Broadcast192.168.1.255Inet6 fe80::20c:29ff:fedf:de30 Prefixlen -ScopeID0x20<link>

This can only be valid at the moment, want to be permanently effective, you must add the variable that you just set in the last side of/etc/profile, save the exit, and the source will be executed permanently after it is in effect.

Vim/etc/profile last append all users to a valid restart or source to use

Or just the root user, vim/root/.bash_profile at the end of this file append

Each user also has their own environment variables in their own host directory

Positional variables

In order to use the shell script: convenient to provide parameters through the command-action Program Bash refers to the concept of positional variables

When you perform a command-line operation, the first field represents the command word or program name, and the remaining string parameters are assigned to the position variable one time, in left-to-right order

Positional variables are also called positional parameters, using $1,$2,$3.....$ to represent

Eg: the script that writes an addition operation illustrates the position variable

Vim zq.sh

#!/bin/bashsum=$ (expr $1 + $2)echo"$+ $ = $SUM "

Test results

[Email protected] ~]# vim ZQ. SH  ~]#./zq. SH bash:. /zq. SH  chmod u+x zq. SH  ~]#./zq. SH Expr  +  =~]#./zq. SH 1 2  Assigning a value to a variable can be 123

Pre-defined variables

$#: Number of positional variables in the command line (the program executes several positional parameters)

$*: The contents of all positional variables (specific content such as/boot is a specific content)

$?: The state returned after the previous command was executed, when the return status value is 0 for normal execution, and a value other than 0 indicates that an exception was performed or the error value is between 1-127

$: The currently executing process/program name (which is the name of the currently executing command or program)

!$: Represents the last parameter of the previous command

Linux Special Shell variables

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.