我想在GNU最新别名gdate
来date
当程序在Mac上运行
#!/bin/bash if [[ $(uname) -eq 'Darwin' ]]; then alias date="gdate" echo 'you are on a mac!' type date fi # rest of the program
鉴于此代码,如果我直接在终端上运行int它打印:
you are on a mac! date is an alias for gdate
但是,如果我像./test.sh
打印一样运行脚本本身:
you are on a mac! date is /bin/date
为什么不从脚本中应用别名?
默认情况下,别名不会在非交互式shell中展开.请改用功能.
if [[ $(name) -eq Darwin ]]; then date () { gdate "$@"; } echo 'you are on a mac!' type date fi