小编aug*_*aug的帖子

如何在 shell 中向 .bash_profile/.profile/bashrc 添加函数?

我有一个将纪元时间转换为日期的函数。这是定义

date1(){
  date -d @$1
}
Run Code Online (Sandbox Code Playgroud)

我希望能够写:

$ date1 xxxyyy
Run Code Online (Sandbox Code Playgroud)

其中 xxxyyy 是我传递给函数的参数,因此我可以获得相应的日期。我明白我必须将它添加在任.bash_profile.profile.bashrc然后它来源:

$ source file
Run Code Online (Sandbox Code Playgroud)

但是,我不确定应该把它放在哪个文件中。目前,我把它放在.profile. 但是要运行它,我必须source .profile每次都这样做。

理想情况下,当计算机像环境变量一样启动时,它应该使其可用。

shell bash profile function

32
推荐指数
3
解决办法
9万
查看次数

bash 中的 grom() 关键字抛出意外的 '(' 标记

我不完全确定为什么我在我的 .bash_profile

syntax error near unexpected token `('
Run Code Online (Sandbox Code Playgroud)

当我将关键字grom()用于我的函数时。我想创建一个 bash 函数,它会自动使用 origin 重新设置我的主分支

# git status
alias gs='git status'

# git stash list
alias gsl='git stash list'  

grom() {
  branch_name="$(git symbolic-ref --short -q HEAD)"
  git fetch && git rebase origin/master && git checkout master && git rebase origin/master && git checkout $branch_name
}

# git stash apply. must append stash@{num}
alias gsa="git stash apply"
Run Code Online (Sandbox Code Playgroud)

当我更改函数的名称时,它编译得很好。我找不到grom关键字,所以我不确定问题是什么。如果我将该函数重命名为其他任何名称git-rom,甚至是类似的名称groms,则它可以正常编译。是否有一些特殊的关键字不起作用?这是在 Mac OS X 上。

bash alias shell-script profile function

3
推荐指数
1
解决办法
605
查看次数

标签 统计

bash ×2

function ×2

profile ×2

alias ×1

shell ×1

shell-script ×1