Bash Shell and variable explanation

Source: Internet
Author: User
Tags aliases i18n locale

Structure of this article: Bash shell Introduction--variables and variable operations, array-->bash Environment profile analysis

What is a shell:

    • Operating system is a set of software to control the entire hardware and management system activities, if the direct use of the operating system, improper operation will cause the system crashes, the user is not free to operate , so through the "shell" of our input command and the kernel communication , so that the kernel accurate Control hardware Work

    • The shell is an application developed on the operating system and is the outermost layer that the user can manipulate, the shell

What is bash

    • Bourne Again Shell,bourne SHell's enhanced version

    • Shell has many versions, many things are the same, there will be a lot of development, like the operating system there are many kinds of truth

    • Shell Types View:

[[email protected] ~]# cat /etc/shells//File records all legitimate shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/dash
/bin/tcsh
/bin/csh

    • The shell is obtained according to/ETC/PASSWD records when the user logs on

[email protected] ~]# cat /etc/passwd
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Bash Shell features

    • Command Memory (History)

1: Up to 1000 articles

2:[-c Delete Current history] [-a synchronous file] [-N recent N Records]

3: History in the ~/.bash_history file (previous command before login, this time cache in memory)

    • Complement function (TAB)

    • Aliases (alias)

    • Job control, front end FG, backstage BG

    • Script (shell script)

    • wildcard character (Wildcard)

    • Built-in command type [-a View all sources of command] [-t view command execution first source] command

The variables under Linux

1: What is a variable : a set of words or symbols that exist in the memory space to replace some settings or data, especially complex or variable data

    • Example the value of the 1:path variable is/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local

    • Example 2:mail variable is used to access a mailbox, when a different user logs into the shell, Mail assigns the corresponding user's mailbox, such as the Boxin User Mail=/var/spool/mail/boxin,mail command to use the mail variable

2: Variable setting rules

    • Variable name cannot begin with a number

    • Single double quotes

[[email protected] ~]# lang= "Lang is $LANG" //double quote, hold feature
[Email protected] ~]# echo $lang
Lang is en_US. UTF-8
[[email protected] ~]# lang= ' lang is $LANG '//single quote, plain text
[Email protected] ~]# echo $lang
Lang is $LANG

    • Anti-single quotes ' and $ ()

[[email protected] ~]# version=$ (uname-r)//Command execution result as variable as external input information
[Email protected] ~]# echo $version
2.6.32-431.el6.x86_64
[Email protected] ~]# version= ' uname-r '
[Email protected] ~]# echo $version
2.6.32-431.el6.x86_64

    • Delete variable contents

${Variable #} delete the " shortest " After the replacement text from the beginning.

${variable # #} from the beginning, delete the word "longest" after the replacement text.

${variable%} from the back , delete the word " shortest " after the replacement text.

${variable%} from the back , delete the word "longest" after the replacement text.

    • Variable substitution

${variable/a/b} replaces the first A with B

${variable//a/b} replaces all A to B

    • Variable content testing and substitution (whether empty or nonexistent)

Aname=${name-newname}//If Name does not exist, then change to NewName

Aname=${name:-newname}//If Name does not exist or is empty, change to NewName

Aname=${name=newname}//Direct name change to NewName

    • Add Variable Content

[Email protected] ~]# version= "$version" Hello
[Email protected] ~]# echo $version
2.6.32-431.el6.x86_64Hello
[Email protected] ~]# Version=${version}hello
[Email protected] ~]# echo $version
2.6.32-431.el6.x86_64Hellohello

    • Cancel variable : unset variable name

3: Environment variables

    • ENV: (Environment) View all environment variables under this shell

    • Set: View environment variables + custom variables

    • Export: Changing a custom variable to an environment variable

4: Language variable locale (affects display and results)

[Email protected] ~]# locale//usr//lib//locale
Lang=en_us. UTF-8//Main language settings, other language variables will be replaced by these two variables (lang lc_all), general settings Lang OK
Lc_ctype= "en_US. UTF-8 "
Lc_numeric= "en_US. UTF-8 "

[email protected] ~]# cat /etc/sysconfig/i18n
Lang= "en_US. UTF-8 "
Sysfont= "Latarcyrheb-sun16"
5: Variable read: read [[email protected] ~]# read Atest
This is a test
[Email protected] ~]# echo $atest
This is a test

[[email protected] ~]# read-p "Your name:" Named//-p prompt-T number of seconds
Your name:boxin
[Email protected] ~]# echo $named
Boxin
6: Arrays : Examples illustrate that the difference between an associative and a general array is that a number starting with an index of 0 becomes a custom

    • General Array

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/CC/wKiom1WIFn-jjTVdAACROT-ZCVg613.jpg "style=" float: none; "title=" Image.png "alt=" Wkiom1wifn-jjtvdaacrot-zcvg613.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/C8/wKioL1WIGDehKaRBAABbLpt8X74539.jpg "style=" float: none; "title=" Image1.png "alt=" Wkiol1wigdehkarbaabblpt8x74539.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/CC/wKiom1WIFoCTXjtTAAA6OwRhXhY447.jpg "style=" float: none; "title=" Image2.png "alt=" Wkiom1wifoctxjttaaa6owrhxhy447.jpg "/>

    • Associative arrays

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/C8/wKioL1WIGDeBikXQAADRsiz2hbA832.jpg "style=" float: none; "title=" Image3.png "alt=" Wkiol1wigdebikxqaadrsiz2hba832.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/CC/wKiom1WIFoCQ0AOHAABOf5l6v6w192.jpg "style=" float: none; "title=" Image4.png "alt=" Wkiom1wifocq0aohaabof5l6v6w192.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/C9/wKioL1WIGDexXuFdAABPr_rvBYc652.jpg "style=" float: none; "title=" Image5.png "alt=" Wkiol1wigdexxufdaabpr_rvbyc652.jpg "/>

Bash Environment configuration file

structure diagram :

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/CC/wKiom1WIFoDjxkT4AADTQDjPLNw839.jpg "style=" float: none; "title=" Image6.png "alt=" Wkiom1wifodjxkt4aadtqdjplnw839.jpg "/>

Login Welcome Information

    • Pre-logon/etc/issue

    • /ETC/MOTD after Login

Login Shell

    • Full login process required to get BASH: Enter account password

Non-login Shell

    • No need to enter account password again: Enter sub-process, etc.

/etc/profile(System overall settings, login shell calls) call the following three file directories

    • /etc/inputre (user has no custom key function, TAB key, etc.)

    • /etc/profile.d/*.sh (file at end of. Sh in directory will be called, all users share command aliases)

    • /etc/sysconfig/i18n (Language Settings)

~/.bash_profile(personal config file, login shell will read)
~/.BASHRC(umask value and PS1 variable according to UID specification, no-login Shell reads)

    • Read/etc/profile.d/*.sh

    • Read/ETC/BASHRC (Redhat only)


This article is from the "Call Me boxin" blog, so be sure to keep this source http://boxinknown.blog.51cto.com/10435935/1664229

Bash Shell and variable explanation

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.