Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

7
  • 31
    the reason is that your script spawns a new shell process as a child of the current shell. Any environment changes you make in the child process cannot affect the parent. When you use . or source, you are not spawning a new child process, you are running the commands in the current shell. Commented Jan 27, 2012 at 20:32
  • 2
    @glennjackman I have a similar problem and I have tried your solution but it logs me off from shell when I do . or source. Why is this happening ? Commented Feb 21, 2012 at 13:03
  • 8
    @Patryk: maybe your script has an exit statement, so it is not suitable to be sourced. Commented Feb 21, 2012 at 13:24
  • 1
    While source ./script works completely fine, sudo source ./script.sh says sudo: source: command not found. How can I do this using sudo? Commented Sep 27, 2014 at 13:37
  • 1
    @71GA: depending on compilation preferences for sudo and depending on configuration settings in /etc/sudoers you can or cannot preserve your environment when running commands with sudo. I suggest you to try to source your script, and then run sudo with -E option to preserve the environment. If it does not work, I suppose there is very little you can do. Commented Sep 27, 2014 at 13:52