Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

test.sh

mkdir test
echo $1
echo $2

It appears that following two commands do the same things. Are they the same? If not, how could I alter my example to show the difference?

cat test.sh | bash -s hi bye
source test.sh hi bye
share|improve this question
    
Is this a homework question? –  mdpc Jun 10 '13 at 18:18
    
@mdpc meta.askubuntu.com/questions/6520/… –  Stuart Jun 10 '13 at 22:31

1 Answer 1

up vote 4 down vote accepted

The difference will be if you are trying to alter the environment of your current shell (i.e. set a shell or environment variable). For example

$ cat test.sh
myVariable=foo
$ unset myVariable; source test.sh; echo ${myVariable:-null}
foo
$ unset myVariable; bash -s < test.sh; echo ${myVariable:-null}
null

This is what source is for: evaluate a script in the current shell. When you launch a new bash process, it cannot alter the parent process's environment.

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.