實用的Shell(二)

本文的主線 Shell => Profile=> Env

本文環境基於Ubuntu1604 => vagrant up

Shell

echo $SHELL
# /bin/sh
echo $0
# -sh

ls -l /bin/sh
# lrwxrwxrwx 1 root root 4 Feb 18  2016 /bin/sh -> dash
cat /etc/shells
# /bin/sh
# /bin/dash
# /bin/bash
# ...

grep op /etc/passwd
# op:x:1002:1002::/home/op:
sudo usermod --shell /bin/bash op
grep op /etc/passwd
# op:x:1002:1002::/home/op:/bin/bash
  • 退出後重新登錄
echo $SHELL
# /bin/bash
echo $0
# -bash

bash

echo $SHELL
# /bin/bash
echo $0
# bash

Profile

cat /etc/profile
# $PS1
# /etc/profile.d/*.sh
cat ~/.profile
# $HOME/.bashrc
cat ~/.bashrc
# $PS1
# alias
# PATH
# JAVA_HOME

sudo vim /etc/profile
# TEST="/etc/profile"
vim ~/.profile
# TEST+=":~/.profile"
vim ~/.bashrc
# TEST+=":~/.bashrc"

Env

  • 退出後重新登錄
echo $TEST
# /etc/profile:~/.bashrc:~/.profile
echo $0
# -bash

bash

echo $TEST
# :~/.bashrc
echo $0
# bash

bash --login

echo $TEST
# /etc/profile:~/.bashrc:~/.profile
echo $0
# bash
login shell non-login shell
定義 A login shell is a shell given to a user upon login into their user account. This is initiated by using the -l or --login option, or placing a dash as the initial character of the command name, for example invoking bash as -bash Non-login shell, also called a sub shell is a shell started after the login process without the -l or --login option and without an extra dash before the command name
配置 it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist
使用 Accessing your computer remotely using ssh
Simulating an initial login shell with bash -l or sh -l
"su -"切換至指定用戶的login shell
其他

互動: 執行shell腳本時的TEST值?

vim test.sh
#! /bin/bash
echo $TEST
chmod +x test.sh

./test.sh

參考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章