Variables in Linux
Tháng Bảy 26, 2008
The env, set, and printenv commands display all environment variables and their values. env and set are also used to set environment variables and are often incorporated directly into the shell
set : hiển thị cả environment variables & shell variables (các giá grị này được khởi tạo ban đầu bởi file /etc/bashrc)
env/printenv : chỉ hiển thị environment variables (các giá grị này được khởi tạo ban đầu bởi file /etc/profile)
Shell Variable (lệnh set bao gồm shell & environment variables) : Variable trong một shell chỉ có giá trị trong shell đó. Khi exit khỏi shell này, variables bị hủy.
Environment Variable (lệnh printenv, env, export trong bash hoặc setenv trong csh) : Variable trong environment có giá trị trong mọi shell thuộc về account đó. Tạo shell nào ra (với cùng account và cùng environment variables) cũng mang các variables ấn định sẵn như nhau.
Hiểu tổng quan thì environment variable là permanent còn shell variable chỉ có giá trị tức thời trong shell đó
let : Perform arithmetic on shell variables
In Linux (Shell), there are two types of variable:
(1) System variables – Created and maintained by Linux itself. This type of variable defined in CAPITAL LETTERS.
(2) User defined variables (UDV) – Created and maintained by user. This type of variable defined in lower letters.
You can see system variables by giving command like $ set, some of the important System variables are:
|
System Variable
|
Meaning
|
| BASH=/bin/bash | Our shell name |
| BASH_VERSION=1.14.7(1) | Our shell version name |
| COLUMNS=80 | No. of columns for our screen |
| HOME=/home/vivek | Our home directory |
| LINES=25 | No. of columns for our screen |
| LOGNAME=students | students Our logging name |
| OSTYPE=Linux | Our Os type |
| PATH=/usr/bin:/sbin:/bin:/usr/sbin | Our path settings |
| PS1=[\u@\h \W]\$ | Our prompt settings |
| PWD=/home/students/Common | Our current working directory |
| SHELL=/bin/bash | Our shell name |
| USERNAME=vivek | User name who is currently login to this PC |
*note : /etc/profile là global file để set value của các environment variables & set các startup program. Còn /etc/bashrc là global file để set default permission, permanent aliases và permanent functions
Statup script for Login shell :
+ /etc/profile : global file chứa các environment variable và các startup program (affect for all user)
+ /etc/bashrc : global file chứa các default permission, permanent alias và các permanent function (affect for all user)
+ /etc/profile.d
+ ~/.bash_profile (affect for specific user)
+ ~/.bashrc (affect for specific user)
Statup script for Non-Login shell :
+ ~/.bashrc
+ /etc/bashrc
+ /etc/profile.d
+ ~/.bash_logout (affect for specific user)