Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I know this must be something silly but somehow I am stuck trying to create a shortcut (alias) for a shell function.

Here is my exact .bash_profile:


[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
alias l="ls -G"
r () {
   echo "iron_worker upload $1"
   echo "iron_worker queue $1"
}
export -f r

So I've added my function r() at the end of the file. Now when I type r in the shell, I still get error:

-bash: r: command not found

I have restarted terminal, shell window, and still no effect.

I am on Mac OS 10.8.5 if that matters.

EDIT.

After some searching found this question suggesting to use bash_profile instead of bashrc, which is now working! But ... what I get are the commands listed but not executed:

Dmitris-MacBook-Pro:~ dmitrizaitsev$ r
iron_worker upload 
iron_worker queue 

This the normal command execution that I expected:

Dmitris-MacBook-Pro:~ dmitrizaitsev$ iron_worker upload
Please specify code package name or path to workerfile
usage: iron_worker upload CODE_PACKAGE_NAME_OR_PATH_TO_WORKERFILE [OPTIONS]
    -n, --name NAME                  override code name
    -c CONCURRENCY,                  max number of concurrent workers for this code package
        --max-concurrency
    -r, --retries NUM_RETRIES        max number of automatic retries on task fail
    -d RETRIES_DELAY,                delay between each automatic retry
        --retries-delay
        --worker-config CONFIG_FILE  config file for worker
    -h, --host HOST                  host name, eg: www.mydomain.com
    -a, --async                      don't wait for package build
        --full-remote-build          activate full remote build
        --config CONFIG              config file
    -e, --env ENV                    environment
        --project-id PROJECT_ID      project id
Dmitris-MacBook-Pro:~ dmitrizaitsev$ 

Instead I am only getting the command and nothing else. Any idea what is going on?

share|improve this question
    
try sourcing your .bashrc file –  MariusMatutiae Dec 16 '13 at 10:21
    
@MariusMatutiae The file is in my user directory ~/ and the content is as in the question. Is that what you mean? –  Dmitri Zaitsev Dec 16 '13 at 10:32
    
What I mean is: sh .bashrc –  MariusMatutiae Dec 16 '13 at 10:32
    
@MariusMatutiae Did that - still the same result :( –  Dmitri Zaitsev Dec 16 '13 at 10:38
    
lol which shell are you using right now? –  MariusMatutiae Dec 16 '13 at 11:06

2 Answers 2

up vote 1 down vote accepted

You should use, inside the same file, the line:

   export -f r

Edit:

To check that this works, you must use a different shell that you have started after you sourced your .bashrc, so that it can have inherited the niew function. In staler shells, this will not work.

Also, insert this

   if [ -f ~/.bashrc ]; then
       source ~/.bashrc
   fi

in your ~/.bash_profile.

If you wish to run the program iron_worker, then the body of the function r should be:

   r () {
   iron_worker upload $1
   iron_worker queue $1
   }

Also, I suggest you provide the full pathname to the command iron_worker, i.e. subsitute iron_worker with /path/to/iron_worker.

share|improve this answer
    
Added and still no effect. Also noticed that aliases like alias l="ls" inside the same file don't work either :( –  Dmitri Zaitsev Dec 16 '13 at 11:04
    
Ok, I figured I need to source .bashrc, then it works. But it isn't automatically activated in the new shell unless I do it again :( Is there a way to source it automatically? –  Dmitri Zaitsev Dec 16 '13 at 11:24
    
Somehow what I get is the opposite - it works in the same window after sourcing but not in a new one. –  Dmitri Zaitsev Dec 16 '13 at 11:41
    
@DmitriZaitsev pls check my new edit. –  MariusMatutiae Dec 16 '13 at 11:46
    
Please see my edit - maybe the function definition is a problem? –  Dmitri Zaitsev Dec 16 '13 at 12:08

Terminal and iTerm open new shells as login shells by default, so bash reads ~/.bash_profile but not ~/.bashrc.

You can either:

  • Use ~/.bash_profile instead of ~/.bashrc.
  • Add . ~/.bashrc to ~/.bash_profile.
  • Make new shells open with a non-login shell, by for example changing "Shells open with" from "Default login shell" to the command /bin/bash in Terminal.

I have done the last two. For example tmux and the shell mode in Emacs open new login shells as non-login shells by default. ~/.bash_profile is still read when I ssh to my computer.

See https://www.gnu.org/s/bash/manual/html_node/Bash-Startup-Files.html for more information.

The function keyword in your function definition is redundant by the way. function name() { :; } is identical to name() { :; } in bash. The function keyword is not defined by POSIX.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.