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.

My question is:
why if I run some file with name aliases for example with content such as:

alias lsa="ls -a" 

directly:

$ ./aliases

it don't create the alias (may be only in script context).
But if I run it with command "source":

$ source aliases

it do the work? I mean after execution the alias "lsa" existing in context of command shell?
"man source" give: "No manual entry for source", and in google I just found that it runs Tcl, but why Tcl influence shell context and bush not?

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

Basically because when you run ./aliases, it creates a process in which your aliases exist but ends up immediately after, whereas when you source it, it applies to your current bash process.

To get help on source, you need to read the man bash. To save you the trouble:

source filename [arguments]
    Read and execute commands from filename in the current shell environment
    and return the exit status of the last command executed from filename.
    If filename does not contain a slash, file names in PATH are used to find
    the directory containing filename. The file searched for in PATH need not
    be executable. When bash is not in posix mode, the current directory is
    searched if no file is found in PATH. If the sourcepath option to the shopt
    builtin command is turned off, the PATH is not searched. If any arguments
    are supplied, they become the positional parameters when filename is
    executed. Otherwise the positional parameters are unchanged. The return
    status is the status of the last command exited within the script (0 if
    no commands are executed), and false if filename is not found or cannot
    be read.
share|improve this answer
2  
Or man csh, as appropriate. –  Scott Oct 1 '12 at 23:30
add comment

When you run it as an executable script, the shell forks a copy of itself as a child process to run the script. This means that any changes to the aliases:

  • Will only be seen by that child.
  • Are lost as soon as the child exits.

By contrast, when you source the script:

  • It is run in the same process (as if you'd simply typed in its contents)
  • Changes to the aliases are retained.
share|improve this answer
add comment

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.