Why does `source .bashrc` complain "command not found"?

Discussion in 'Ruby' started by Adam Akhtar, Jan 27, 2010.

  1. Adam Akhtar

    Adam Akhtar Guest

    Is it just me or is this something that just cant be done?

    Ive found out that whenever system calls are made they open a child
    shell process. And as soon as that call is finished the child shell is
    closed and any changes that were made to the environment are lost with
    it.

    But i should still be able to execute source .bashrc (even though the
    changes will be lost as soon as that call finishes). Instead I get
    command not found. How come?

    (Ubuntu 9.04 and Ruby 1.8x)
    --
    Posted via http://www.ruby-forum.com/.
    Adam Akhtar, Jan 27, 2010
    #1
    1. Advertising

  2. On Wed, Jan 27, 2010 at 10:11 AM, Adam Akhtar <> wrote:
    > Is it just me or is this something that just cant be done?
    >
    > Ive found out that whenever system calls are made they open a child
    > shell process. And as soon as that call is finished the child shell is
    > closed and any changes that were made to the environment are lost with
    > it.
    >
    > But i should still be able to execute source .bashrc (even though the
    > changes will be lost as soon as that call finishes). Instead I get
    > command not found. How come?
    >
    > (Ubuntu 9.04 and Ruby 1.8x)


    irb(main):004:0> `echo $0`
    => "sh\n"

    and sh doesn't have a source command. I remember there was a question
    recently about changing the shell the backticks use, but I can't
    remember the answers, maybe you can search for it in the archives?

    Jesus.
    Jesús Gabriel y Galán, Jan 27, 2010
    #2
    1. Advertising

  3. Adam Akhtar wrote:
    > Is it just me or is this something that just cant be done?

    ...
    > (Ubuntu 9.04 and Ruby 1.8x)


    In recent Ubuntu versions, the default shell is dash (/bin/sh is a link
    to /bin/dash), and 'source' is not a known keyword in that shell. To
    demonstrate:

    $ /bin/sh
    $ source /dev/null
    /bin/sh: source: not found
    $ exit

    The solution: use '.' instead of 'source'.

    dash is a POSIX-compatible shell without lots of non-standard bash
    extensions. If you really need to use bash-isms, then you should invoke
    bash explicitly.

    >> `/bin/bash -c 'source /dev/null'`

    => ""
    >>


    HTH,

    Brian.
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Jan 27, 2010
    #3
  4. Adam Akhtar

    Adam Akhtar Guest

    Thank you very much for both of your replies. I just tried

    `/bin/bash -c 'source whatever'`

    and it worked! Fantastic.

    But i don't understand if source isnt a known command then how come when
    i go to my command line and type source it works fine?

    Thats why i was initially confused. It worked manually but not inside a
    script or in irb.

    --
    Posted via http://www.ruby-forum.com/.
    Adam Akhtar, Jan 27, 2010
    #4
  5. Adam Akhtar

    Ammar Ali Guest

    Adam Akhtar wrote:
    > Thank you very much for both of your replies. I just tried
    >
    > `/bin/bash -c 'source whatever'`
    >
    > and it worked! Fantastic.
    >
    > But i don't understand if source isnt a known command then how come when
    > i go to my command line and type source it works fine?
    >
    > Thats why i was initially confused. It worked manually but not inside a
    > script or in irb.
    >
    >



    source is a built-in shell command, not an executable. It is only
    available from within the shell. When you go to your command line,
    you're in the shell. In a ruby script, or irb, the above works because
    it runs the bash shell and hands it the commands to run as a string (the
    -c option).

    Try running these:

    $ which bash
    # /bin/bash

    $ which source
    # returns nothing

    HTH,
    ammar
    Ammar Ali, Jan 27, 2010
    #5
  6. Adam Akhtar wrote:
    > Thank you very much for both of your replies. I just tried
    >
    > `/bin/bash -c 'source whatever'`
    >
    > and it worked! Fantastic.
    >
    > But i don't understand if source isnt a known command then how come when
    > i go to my command line and type source it works fine?


    because your login shell is /bin/bash not /bin/sh (grep for your
    username in /etc/passwd)

    The correct solution is not to use bash-specific functionality:

    `. whatever`

    should work just fine. By doing this you'll help your script to be
    portable to other operating systems, like *BSD which tend not to install
    bash by default because of its restrictive GPL licence.

    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Jan 27, 2010
    #6
  7. Ammar Ali wrote:
    > source is a built-in shell command, not an executable. It is only
    > available from within the shell.


    You are correct, but you've missed the point. As the OP already
    observed, ruby *does* invoke a shell when you pass it a string in
    backticks.

    The point is that his default shell doesn't have a 'source' builtin.

    `source /dev/null` # fails (not a POSIX shell feature)
    `. /dev/null` # works

    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Jan 27, 2010
    #7
  8. Adam Akhtar

    Phil Romero Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Wed, Jan 27, 2010 at 2:04 AM, Ammar Ali <> wrote:

    > Adam Akhtar wrote:
    >
    >> Thank you very much for both of your replies. I just tried
    >>
    >> `/bin/bash -c 'source whatever'`
    >>
    >> and it worked! Fantastic.
    >>
    >> But i don't understand if source isnt a known command then how come when i
    >> go to my command line and type source it works fine?
    >>
    >> Thats why i was initially confused. It worked manually but not inside a
    >> script or in irb.
    >>
    >>
    >>

    >
    >
    > source is a built-in shell command, not an executable. It is only available
    > from within the shell. When you go to your command line, you're in the
    > shell. In a ruby script, or irb, the above works because it runs the bash
    > shell and hands it the commands to run as a string (the -c option).
    >
    > Try running these:
    >
    > $ which bash
    > # /bin/bash
    >
    > $ which source
    > # returns nothing
    >
    > HTH,
    > ammar
    >
    >
    >

    Another good tool to use is 'type' when trying to puzzle this stuff out:

    [pdr@grace]$ type source
    source is a shell builtin
    [pdr@grace]$ type .
    is a shell builtin
    Phil Romero, Jan 27, 2010
    #8
  9. Adam Akhtar

    Adam Akhtar Guest

    Many thanks for your replies again - most helpful and greatly
    appreciated. Saved a few hours of my hitting my head against a brick
    wall.


    --
    Posted via http://www.ruby-forum.com/.
    Adam Akhtar, Jan 27, 2010
    #9
  10. Adam Akhtar

    Seebs Guest

    On 2010-01-27, Adam Akhtar <> wrote:
    > Is it just me or is this something that just cant be done?
    >
    > Ive found out that whenever system calls are made they open a child
    > shell process. And as soon as that call is finished the child shell is
    > closed and any changes that were made to the environment are lost with
    > it.
    >
    > But i should still be able to execute source .bashrc (even though the
    > changes will be lost as soon as that call finishes). Instead I get
    > command not found. How come?


    External shell almost always uses /bin/sh, which is usually more POSIXY,
    and "source" is a bash-only feature. Try ". $HOME/.bashrc" or something
    close to that -- the "." command often requires a path to reach things
    in the current directory, to avoid certain obvious failure modes.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Jan 27, 2010
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roy Yao
    Replies:
    4
    Views:
    377
    Roy Yao
    Aug 21, 2003
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,805
    Smokey Grindel
    Dec 2, 2006
  3. thinktwice
    Replies:
    3
    Views:
    334
    Daniel T.
    Feb 10, 2006
  4. Slan Dizier
    Replies:
    0
    Views:
    103
    Slan Dizier
    Apr 29, 2009
  5. Ronny
    Replies:
    3
    Views:
    185
    Ronny
    Feb 24, 2008
Loading...

Share This Page