Echo在无引号,双引号,单引号下的扩展
通配符号扩展 | 变量扩展 | |
无引号 允许所有扩展 没有限制 | echo *.txt [macg@machome ~]$ sh test.sh 111.txt 22.txt 33.txt | echo $SHELL /bin/bash |
双引号 可以防止通配符扩展 但双引号允许变量扩展和$()扩展 | echo "*.txt" [macg@machome ~]$ sh test.sh *.txt | echo "$SHELL" /bin/bash |
单引号 禁止所有扩展 | echo '*.txt' [macg@machome ~]$ sh test.sh *.txt | echo '$SHELL' $SHELL |
单引号会硬性显示引号内所有字符,不考虑特殊字符
echo –n 不输出回车 ,常用于read前的提示符
[root@localhost etc]# echo haha haha |
[root@localhost etc]# echo -n haha haha[root@localhost etc]# |
最基本的shell写文件方法----- echo >>
echo "aaaa" >> jia
echo "bbbb" >> jia
echo "cccc" >> jia
echo "dddd" >> jia
echo "eeee" >> jia
$ cat jia
aaaa
bbbb
cccc
dddd
eeee
用exec 标准输入文件+ echo写文件
$ vi in.sh exec 8>jiagebiao echo "chicken_price=$chicken" >&8 echo "duck_price=$duck" >&8 echo "fish_price=$fish" >&8 |
$ cat jiagebiao chicken_price= duck_price= fish_price= |
标准命令行输入——read
echo -n "input:" read para echo "$para" |
[root@mm testtip]# sh test.sh input:aaa aaa |
read < file 直接从文件读数据到变量
[root@mm testtip]# vi test.sh #!/bin/bash echo -n "input:" read para read filepara <./$para.pass echo "$filepara" |
[root@mm testtip]# cat ten.pass 1234567 [root@mm testtip]# sh ./test.sh input:ten 1234567 |
tee -------相当于read 和echo > file的组合:提供从键盘读入文件的功能
tee相当于read + echo > 从STDIN读一段字符串,写入文件中 [root@nm testsh]# tee test.txt hello hello 反显回屏幕 [root@nm testsh]# cat test.txt 同时写入文件 hello |
tee -a相当于read + echo >> [root@nm testsh]# tee -a test.txt i m mac i m mac who are you who are you [root@nm testsh]# cat test.txt hello i m mac who are you 追加入文件 |
export 变量只能作用于子进程,不能作用于同辈进程。
(即在一个shell中export某变量,退出shell后该export变量不存在,但在该shell中执行另一个子shell或子进程,则该export变量继续存在)
[root@vm test]# vi test.tip.sh TEST="haha" GO="gogo" echo -n "display from testtip" echo $TEST export TEST GO echo $TEST $GO echo =============== ./tip.sh 在脚本中调用另一个脚本 [root@vm test]# vi tip.sh #!/bin/bash echo $TEST $GO 调用上一脚本的变量 |
在脚本中调用另一个脚本, 则export变量传递 [root@vm test]# sh ./test.tip.sh display from testtiphaha haha gogo =============== haha gogo |
单独直接调用那个外部脚本,则export变量不存在 [root@vm test]# ./tip.sh [root@vm test]# |
unix所有命令行环境,都是在某一个父进程fork出新的子进程,这样才能实现环境变量的export 父子传递
由于export变量的父子传递特性,如果想新建一个unix环境变量,必须到/etc/profile 或/etc/bashrc 或~/.bashrc ~/.profile,相当于父环境
在其他地方定义和export都不会成功
Source 命令——修改/etc/profile立刻生效
vi /etc/profile
source /etc/profile 这样,刚刚写进去的环境变量就生效了
其实退出shell再登进去,应该/etc/profile就生效了