0

I have these functions in my .bash_profile on ubuntu server instance on google cloud for pull and push the git branch

plb(){
        branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
        git pull origin $branch
}
psb(){
        branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
        git push origin $branch
}

if i run the code line individually then the work properly but when i try to put it in a function it shows the following error

$ plb
 : command not found
 : command not found

what am i doing wrong here, thank you in advance

Output of the command:-

rohan@staging:~$ proj
rohan@staging:/var/www/staging/Server/www$ branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
rohan@staging:/var/www/staging/Server/www$ echo $branch
staging
rohan@staging:/var/www/staging/Server/www$ plb
 : command not found
 : command not found
rohan@staging:/var/www/staging/Server/www$ 
2
  • you need to export your functions. Try adding export -f plb and export -f psb in your .bash_profile Commented Feb 8, 2017 at 11:20
  • @Aserre added the export but it is not working still, my .bash_profile looks like this now plb() { branch=$(git branch | sed -n -e 's/^* (.*)/\1/p') git pull origin $branch } psb(){ branch=$(git branch | sed -n -e 's/^* (.*)/\1/p') git push origin $branch } export -f plb export -f psb Commented Feb 8, 2017 at 11:51

1 Answer 1

1

Have you restarted your session? .bash_profile is not re-read automatically when you modify it. You can manually reload if by issuing a command like . ~/.bash_profile at the command prompt.

Functions must be loaded in memory before being called, and calling a function does not automatically causes the file where it is defined to be read (the shell does not know in which file any specific function is located anyway). When the function is defined inside the script from which it is called, its definition must precede its use, for the same reason : the shell will not skip ahead in the script file to find a function that has not yet been defined.

Sign up to request clarification or add additional context in comments.

4 Comments

yes i did restarted the session, also i made sure that i am on the correct directory where project folder is located.
What could be wrong? Are you 100% your .bash_profile is in the right place (your user home directory) and actually gets sourced (not executed as a separate process)? Are your sure it does not exit before reaching your function definitions? Are you sure Bash is the shell used, and is not launched with an option that would prevent .bash_profile from being loaded?
i have other commands in .bash_profile they work properly like alias ..="cd .." its only the function that is not getting executed.
Are your functions called from another script, or at the command line? I think by default, functions are not inherited by sub-processes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.