0

I've written a simple shell script that adds some aliases to a shell it is executed as part of an assignment for school. Here is the contents of the file testscript.txt:

#!/usr/bin

alias dir="ls -l"
alias alerts="cat /etc/motd"
PS1="What next Matt ? "

When I use the following command:

sh testscript.txt

The command appears to run since no errors are returned, however, the alias and command prompt variables have not been changed.

I know that the script works properly because I have tried the following three alternatives, which all worked without a hitch:

. ./testscript.txt
. testscript.txt
source testscript.txt

The assignment requires that I use the sh command. Any suggests to make the command update the aliases and environmental variables?

Here is a screenshot of the assignment so you can see the requirements I must fulfil: enter image description here

Thank you for you time and help.

4
  • 1
    you can't control the environment of the parent from a subshell, which leads me to assume that either (1) . ./testscript is just what they're looking for, or (2) you've misunderstood the assignment or (3) they've incorrectly formulated the assignment (which is less rare then you think..). I would add #!/usr/bin/sh as the shebang instead of #!/usr/bin, that's enough 'sh' in the solution for me..
    – Wrikken
    Oct 17, 2013 at 22:08
  • 1
    @Wrikken: The correct shebang is #!/bin/sh, not (necessarily) #!/usr/bin/sh. The latter may work on some systems; the former is practically guaranteed. Oct 17, 2013 at 23:28
  • @KeithThompson: you are absolutely right, it was just a brainfart here on my part with the obviously out of place #!/usr/bin. I stand corrected ;)
    – Wrikken
    Oct 17, 2013 at 23:30
  • I'm guessing the point of the exercise is to figure out why it doesn't work...? Or maybe just observe that it doesn't, as a platform for further discussions.
    – tripleee
    Oct 21, 2013 at 16:38

2 Answers 2

0

Any environment variables you set up inside a stand-alone script will only be visible from within that script. instead, you need to add these commands to ~/.profile (or whatever its equivalent is on your system). Then they will be executed when you log into the command line console.

1
  • Yes, usually I would set up the environmental variables here or in .bashrc. I posted a screenshot of the assignment so that you're aware of my restrictions.
    – Matt
    Oct 17, 2013 at 22:22
-1

The shebang (first line) is not correct.

In your case, you should replace the first line by either #!/bin/sh or #!/bin/bash.

Just search for 'shebang' in Google. It is basically the absolute path of the launched executable. http://en.wikipedia.org/wiki/Shebang_(Unix)

1
  • -1 While correct and mildly relevant, this is not what the OP is asking about.
    – tripleee
    Oct 21, 2013 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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