0%

A little basic knowledge of shell

I don’t know what to share for ARTS this week. So I summarize a little knowledge of the shell.

What is a shell

A shell is a software interface that is ofthen a command line interface that enables the user to interact with the computer. In linux, we can check all supported shell via the following way.

1
2
3
4
5
6
7
lee@lee-server:~$ sudo cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/dash
/bin/tcsh
/bin/csh

bash( Bourne again shell) - rewrited sh, generating more functions.

What is the differnce between script execution

First situation

Execute as one child process, once the child process exists and back to parent shell, the related envs will disappear. Such as:

1
2
bash script_name.sh
./script_name.sh # Need to add execute permission in advance. like, chmod u+x script_name.sh

envs: environmental variables.

Second situation

Execute in the current shell. envs will always work before quit. Such as:

1
2
source script_name.sh
. script_name.sh

Pip

The output of left cmd is used as the input to the right cmd. As follows:

1
2
3
4
5
6
lee@lee-server:/boot/grub$ cat | ps -f # Create one new process for every external cmd.
UID PID PPID C STIME TTY TIME CMD
lee 21193 4020 2 11:22 pts/3 00:00:00 bash -c cd "/boot/grub" && bash -i -c "cat | ps -f"
lee 21596 21193 4 11:22 pts/3 00:00:00 bash -i -c cat | ps -f
lee 21775 21596 0 11:22 pts/3 00:00:00 cat
lee 21776 21596 0 11:22 pts/3 00:00:00 ps -f

We should avoid to use built-in cmd in pip ,such as , ls , cd ,etc.

built-in cmd: execute in the current shell.
external cmd: create one new process, like top.

Input and output redirection

1
2
3
4
5
6
7
8
9
10
11
"<"   # input redirection.
">" # output redirection
">>" # output append redirection.
"2>" # error output redirection.
"&>" # all output redirection.

# input + output redirection, EOF part redirect as input f cat.
cat > file.txt <<EOF
Hello, Shell!
EOF
# EOF can be any strings, it is only a conventional writing (End Of File)

Variable

Definition of variables

1
2
3
4
5
6
var=value
let var=value
l=ls
var=$(ls -l /etc)
or
var=`ls -l /etc`

Note: ${var}, sometimes we can only use $var, sometimes we can’t, i.e., ${var}test != $vartest.

Scope of the variable

Variable only works in the current terminal or the current shell script.

If we want to use it in the child process, we need to export it as follows:

1
export var(=xxx)

If we don’t need it anymore, we need to clear it as follows:

1
unset var

Env and others

1
2
3
4
5
6
7
8
9
10
11
12
set # display the current shell variable.
env # view all current env.
echo $var # view var.
echo $PS1 # The current prompt terminal
$? # The return value of the previous cmd
$$ # the current process's PID
$0 # the current process name
# Get the parameters passed to the current process
$1 $2 ... ${10} ... ${n}
# Initial for null variable, avoid nothing to display.
echo ${2}_ # if [ -n "$2" ],then: display '_' else display 'value_' fi # value --> value of $2
echo ${2-_} # if [ -n "$2" ],then: display '_' else display 'value' fi

Config of linux

All user’s config

1
2
3
/etc/profile
/etc/profile.d/
/etc/bashrc

Current user’s config

1
2
~/.bash_profile
~/.bashrc

Loading order

su - root

1
2
3
4
/etc/profile
~/.bash_profile
~/.bashrc
/etc/bashrc

It is a better way to switch account.

su root

1
2
~/.bashrc
/etc/bashrc