两种运行方式,终端对话框运行 or 运行脚本文件 bash /path/to/file.script.sh
历史记录存储在 .bash_history 文件中
两个 cmd 可以写在同一行,但是需要用 ; 隔开, echo a ; echo b 等价于 echo a \n echo b
什么是 login shell A login shell is the shell which you get just after logging in to a machine. However, if you open up a shell while logged in to a graphical environment (such as GNOME, KDE, and so on), then it is not a login shell.
Printing in the terminal
1 2 3 4 5 6 7 8 9 10 11
# 打印,自带结尾换行效果 echo"Welcome to Bash" # Welcome to Bash
# 不带引号也可以 echo Welcome to Bash # Welcome to Bash
# 单引号也可以 echo'text in quotes' # text in quotes
从上面效果看,都能打印,而且结果都一致,但是这几种使用方式还是有区别的
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# " 中不能以 ! 结尾, 在 bash 中 !有特殊含义,表示前一个 echo"hi !" # bash: !": event not found
# 调用之前命令测试 echo aaa # aaa !echo # echo aaa # aaa
# 但是如果 ! 后面接空格则正常 echo"hi ! " # hi !
echo 的几种用法的副作用:
光杆 echo, 后面不能接 ; 不然命令断裂了
单引号 echo, 变量复制不起作用
echo 的其他用法:
echo -n "hi" 使用 -n 取出换行
echo -e "1\t2\t3" 使用 -e 打印转义符
echo 输出变色:
1 2 3 4 5 6 7 8 9 10
# \e[1;31m 设置字体为红色, \e[0m 重制 echo -e "\e[1;31m This is red text \e[0m" # This is red text
# \e[1;42m 设置背景为绿色, \e[0m 重制 echo -e "\e[1;42m Green Background \e[0m" # Green Background
# 混合使用也是 OK 的 echo -e "\e[1;42m\e[1;31m Green Background \e[0m"
1 2 3 4 5 6 7 8 9 10 11 12 13
# printf 和 C 语言中用法一致,默认不带换行 bash-3.2$ printf"hello world" hello worldbash-3.2$
# 打印示例 printf"%-5s %-10s %-4s\n" No Name Mark printf"%-5s %-10s %-4.2f\n" 1 Sarath 80.3456 printf"%-5s %-10s %-4.2f\n" 2 James 90.9989 printf"%-5s %-10s %-4.2f\n" 3 Jeff 77.564 # No Name Mark # 1 Sarath 80.35 # 2 James 91.00 # 3 Jeff 77.56
PS: -5, 左对齐,占 5 个位置
Session 3 Playing with variables and environment variables
赋值语句 var=value,需要注意的是,var = value 是不对的,后者是等于运算。如果 value 中间没有空格,则直接写,不然需要用引号包裹。当使用 echo 或者 prinf 输出变量时,需要用双引号
# 如果运行 script 的用户不是 root,打印提示信息 # 注意点: # 1. 书上的例子首字母写错了 # 2. if [ 之间都有空格的,不然会抛语法错误 # 3. root user 的 UID 为 0 if [ $UID -ne 0 ]; then echo Non root user. Please run as root. else echo Root user fi
# 修改后的 \$\{$1:+':'\$$1\} 是什么意思? # :+ 表示覆盖缺省值 # 只有当var不是空的时候才替换成string, 若var为空时则不替换或者说是替换成变量var的值,即空值 COMPANY="Nightlight Inc." echo"${COMPANY:+Company has been overridden}" # Company has been overridden COMPANY= echo"${COMPANY:+Company has been overridden}" # 打印空行
# bash 中也有自带的函数,比如次方和开方 echo"10^2" | bc # 100 echo"sqrt(100)" | bc # 10
man page 说明如下: There are four special variables, scale, ibase, obase, and last. scale defines how some operations use digits after the decimal point. The default value of scale is 0. ibase and obase define the conversion base for input and output numbers. The default for both input and output is base 10. last (an extension) is a variable that has the value of the last printed number. These will be discussed in further detail where appropriate. All of these variables may have values assigned to them as well as used in expressions.
# tee 将输出写入文件的同时,给一份到 stdout # 默认情况下 tee 会覆盖原文件,用 tee -a 可以达到 append 的效果 cat a* | tee out.txt | cat -n # cat: a1: Permission denied # 1 a1 # 2 a1 cat out.txt # a1 # a1
# tee 后接多个 file,内容都是重复的 echo aaa | tee f1 f2 cat f1 f2 # aaa # aaa
# `-` 代表标准输入, echo who is this | tee - # who is this
> Vs >>: 前者是覆盖,后者是续接
./dev/null is a special device file where any data received by the file is discarded. The null device is often known as a black hole as all the data that goes into it is lost forever. /dev/null 是一个特殊的设备,可以将它看作一个黑洞,所有进去的东西都没了
1 2 3 4 5 6 7 8 9 10 11
# 终端多行输入 cat << EOF > log.txt LOG FILE HEADER This is a testlog file Function: System statistics EOF
cat log.txt # LOG FILE HEADER # This is a test log file # Function: System statistics
自定义文件描述符
可用模式:Read mode, Write with truncate mode, Write with append mode
1 2 3 4 5 6 7
# `<` 用于将文件导向 stdin echo this is a test line > input.txt exec 3<input.txt cat <&3 # this is a test line cat <&3 # 没有输出,这种方式不能复用,需要重新赋值
bash --version # GNU bash, version 5.1.8(1)-release (x86_64-apple-darwin20.3.0) # Copyright (C) 2020 Free Software Foundation, Inc. # License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
# This is free software; you are free to change and redistribute it. # There is NO WARRANTY, to the extent permitted by law.
## 安装结束
Associative arrays 使用案例
1 2 3 4 5 6 7 8
declare -A fruits_value fruits_value=([apple]='100 dollars' [orange]='150 dollars') echo"Apple costs ${fruits_value[apple]}" # Apple costs 100 dollars
# 输出下标,或者索引更贴切 echo${!fruits_value[*]} # orange apple
PS: 感觉被骗了,这 TM 叫数组?!!命名就是字典嘛
Visiting aliases
alias: 将很长的命令用一个简写来代替, 声明形式 alias new_command='command sequence', 例如 alias install='sudo apt-get install'
There is no mechanism for using arguments in the replacement text, as in csh. If arguments are needed, a shell function should be used (see Shell Functions).