atguigu9004.Linux之自定义的 Shell 函数和函数库

shell脚本存放位置

不管放到哪个文件夹下,把路径添加到$PATH中即可直接使用;
一般放在 ~/bin中。

$ vim /etc/profiles

添加配置


MYBIN=/home/my/mybin
export PATH=$PATH:$MYBIN

$ source /etc/profiles

自定义shell脚本,定义自己的函数库

如:$ vim ~/bin/show.sh


#!/bin/bash

showDate(){
  date
}

showUser(){
  echo $USER
}

$ chmod u+x show.sh

$ chmod 755 show.sh

调用自定义函数库

如:$ vim test.sh

source show.sh
showDate
showUser
# 或
# . show.sh # 点和文件路径之间有空格

$ chmod u+x test.sh

$ chmod 755 test.sh

$ ./test.sh

参考

https://www.linuxprobe.com/compile-shell-function.html

猜你喜欢

转载自www.cnblogs.com/mylk0606/p/11820416.html