Last updated 5 years ago
Was this helpful?
遇到一个很奇怪的问题,想在shell脚本中使用$PS1中的内容
$PS1
#!/bin/bash INFO=`echo $PS1 | awk '{print $2}'` echo $INFO
结果发现输出是空的,即使直接在脚本中echo $PS1也是空的。
echo $PS1
但是,明明在终端中执行echo $PS1是能够输出内容的。这是为何?
原来shell脚本并不会直接去读取/etc/profile/~/.bashrc这样的环境脚本,需要在脚本开头显式地.引用
/etc/profile
~/.bashrc
.
#!/bin/bash . /etc/profile INFO=`echo $PS1 | awk '{print $2}'` echo $INFO
删除"\n$"失败