Linux_note shell attributes, variables, system, and user's environment variable profiles.

Source: Internet
Author: User
Tags aliases

The shell is a programming language, a command interpreter, which is the kernel that interprets the commands we enter.

He is divided into two types, the interactive mode is that the shell waits for your input, and executes the commands you submit; noninteractive mode, not interacting with you


, instead, read the commands stored in the file and execute them.

The Shell under UNIX is: C shell/bash/sh/ksh/csh; That's all we've come across.

1. Shell Features

The command history file root user is located in the home directory. Bash_history is the/root/.bash_history default save 1000, by changing $histsize


#echo $HISTSIZE viewable.

#!! Execute the previous instruction

!$ $ represents the last parameter of the previous instruction

such as: [[email protected] ~]# ls-l 1.txt

[email protected] ~]# cat!$

Cat 1.txt

#!n execute command history in nth instruction

#! string executes the command at the beginning of the most literal string in the command history

such as:!ca

[Email protected] ~]#!CA

Cat 1.txt


Alias aliases (only in the current shell, to make it effective in other shells requires a configuration file)

Alias command aliases = ' Specific commands '

Such as:

[[email protected] ~]# alias aaa= ' Cat 1.txt '

[[email protected] ~]# alias

Alias Aaa= ' Cat 1.txt '

Alias cp= ' Cp-i '

Alias: Unalias Command alias

#unalias AAA


Wildcard * Matches 0 or more characters

? Match one character

Such as:

[[email protected] ~]# ls *.txt

123.txt 1.txt 2.txt 3.txt a.txt

[email protected] ~]# ls?. Txt

1.txt 2.txt 3.txt A.txt


Pipe Break: | Throw the result of the previous command to the following command

such as: [[email protected] ~]# cat/etc/passwd|wc-l

30


REDIRECT:>,>>,<,2>,2>>

#echo "123" >1.txt > overwrite the contents of the original file 1.txt

[Email protected] ~]# echo "123" >1.txt

[email protected] ~]# cat 1.txt

123

If you do not want to overwrite with append redirect >>

[Email protected] ~]# echo "456" >>1.txt

[Email protected] ~]# echo "789" >>1.txt

[email protected] ~]# cat 1.txt

123

456

789

Reverse redirect < Throw a file content to a command

such as: [[email protected] ~]# Wc-l < 1.txt

3

Error redirection:2>,2>>

[[email protected] ~]# ls 11111

LS: unreachable 11111: No file or directory

[[email protected] ~]# ls 11111 > 1.txt will also overwrite the original file content

LS: unreachable 11111: No file or directory [[email protected] ~]# ls 11111 2> 1.txt

[email protected] ~]# cat 1.txt

LS: unreachable 11111: No file or directory

Do not overwrite the original file content:2>>

[[email protected] ~]# ls 11111 2>> 1.txt

[email protected] ~]# cat 1.txt

LS: unreachable 11111: No file or directory

LS: unreachable 11111: No file or directory


CTRL + Z temporarily stop the current command, #jobs view tasks that are paused or run in the background, #fg恢复, and into the foreground.

[[email protected] ~]# Sleep 100

^z

[1]+ Stopped Sleep 100

[[email protected] ~]# sleep 160

^z

[2]+ Stopped Sleep 160

[[email protected] ~]# jobs

[1]-Stopped Sleep 100

[2]+ Stopped

+ indicates high priority, #fg时调用 +

[[email protected] ~]# FG

Sleep 160

Call 1 or 2: #fg1或fg2

BG moves commands to the background to run

[[email protected] ~]# jobs

[2]-Stopped Sleep 1246

[3]+ Stopped Sleep 1546

[[email protected] ~]# FG

Sleep 1546

^z

[3]+ Stopped Sleep 1546

[Email protected] ~]# BG

[3]+ Sleep 1546 &

[[email protected] ~]# jobs

[2]+ Stopped Sleep 1246

[3]-Running Sleep 1546 &


2, shell variables (System variables and user-defined variables, shell preset variables are uppercase)

System variables: #env或set显示系统环境变量, set displays the information in more detail, contains the custom variables,

To view and reference variables, add $ to the variable name, such as: [[email protected] ~]# echo $HOSTNAME

Zeklinux

Shell Custom Variable rules:

Format: Variable name = variable value equals ' = ' cannot have spaces on either side.

Variable names can only consist of English letters, numbers, and underscores, but cannot begin with a number. (usually lowercase, avoid using system commands, keywords)

Variable values with special characters (such as spaces) need to be added ';

[Email protected] ~]# b= ' ls/tmp/'

[Email protected] ~]# echo $b

If you run the result with another command, use the anti-quote '

[Email protected] ~]# myvim= ' which vim '

[Email protected] ~]# echo $myvim

/usr/bin/vim

[email protected] ~]# which vim

/usr/bin/vim

Variable contents accumulate other variable contents need double quotation mark "":

[Email protected] ~]# d= "$myvim" 3

[Email protected] ~]# echo $d

/usr/bin/vim3

[Email protected] ~]# echo $myvim

/usr/bin/vim

If the contents of a variable refer to two variables, you can write the following: # c= $d $ A

Cancel variable unset variable name:

[Email protected] ~]# unset D

Summary: The anti-quote is used to directly refer to the command inside the inverted quotation marks, if there is a special match for example $, it can recognize its meaning.


If there is a special symbol inside the single quotation mark, it is not recognized.

Declaring global variables

You need to make a global declaration when using variables. Enter bash in a shell and enter the child shell, and the previously defined variable is invalidated in the child shell. Want to make


variable is valid in child Shell use command export

#bash into the child shell

#exit exit the child shell back to the parent shell

#export variable name in parent shell export a variable in child shell that variable takes effect

#bash

#echo $ variable Name

such as: [[email protected] ~]# bash

[[Email protected] ~]# exit

Exit

[[email protected] ~]# Export D

[Email protected] ~]# bash

[Email protected] ~]# echo $d

/usr/bin/vim3

When export does not add any variable names, all variables are declared.

Command: Pstree view current shell in that layer #pstree |grep bash


For all users in the system to be able to use after logging in, you need to add the export variable name = variable content at the bottom of the/etc/profile file and then ship


The line source/etc/profile in effect.


3. System and User's environment variable configuration file

System files used in the system to specify environment variables have path HOME shell, etc.

The custom variable or file is written to the/etc/profile.d/directory and is named. sh file

Write in path.sh

#!/bin/bash

Export path= $PATH:/tmp/:/data/bin/#!/bin/bash

[Email protected] ~]# vim/etc/profile.d/path.sh

[[email protected] ~]# source/etc/profile Reset profile

[Email protected] ~]# echo $PATH

/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/tmp/:/data/bin/

Can see more:/tmp/:/data/bin/

ETC/BASHRC: This file is mainly preset umask and PS1

[Email protected] ~]# echo $PS 1

[\[email protected]\h \w]\$

\u is the user,

\h Host name,

\w is the current directory,

\$ is the ' # ', and if it is a normal user, it is displayed as ' $ '.

w-w ~ becomes absolute path

[[email protected] ~]# ps1= ' [\[email protected]\h \w]\$ '

[Email protected] ~]# cd/etc/init.d/

H plus \ t display time

[[email protected]/etc/init.d]# ps1= ' [\[email protected]\h\t \w]\$ '

[[Email protected]:48:38/etc/init.d]#


. Bash_history: Records the command history.


. bash_logout: When you exit the shell, the file is executed. You can put some cleanup work into this file.

The user's own environment variable profile. Bash_profile and System/etc/profile is a type of file

. BASHRC defines the user's command alias, alias, the file that is executed when the user logs on and every time the shell is opened


Linux modified profile changed the wrong, the method of recovery

When you change the profile, the problem is changed, except for the CD commands are basically not used,

Even VI can not be used, the Internet to check the next,

With Export Path=/usr/bin:/usr/sbin:/bin:/sbin:/usr/x11r6/bin,

Then you can use the command, the speed with the VI to change the profile back, return to normal.

Shell commands are basically defined in/usr/bin,/usr/sbin,/bin,/sbin,/usr/x11r6/bin.


Linux_note shell attributes, variables, system, and user's environment variable profiles.

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.