سؤال

I have write very small script which contain one alias and execute that But it is not executing the command ts. Could anyone please provide me input to fix this issue.

#!/bin/tcsh
alias ts 'cd /path/goswami/;'
ts
هل كانت مفيدة؟

المحلول

Your script is working just fine. It changes the directory but you can't visibly see that when executing the script. After your script has been executed the old working directory will be in effect again.

If you want to make sure that this is indeed the case then you could try the following:

#!/bin/tcsh
alias ts 'cd /path/goswami/; echo `pwd`'
ts

It will print /path/goswami as output.

Update: However, if you want to change the directory "outside" the script then don't use the script at all. Just define your alias and use that instead of invoking your script.

نصائح أخرى

I think this question is about the difference between executing a script and sourcing a script.

If you execute a script by setting the executable permission and then calling it by it's full or relative path, or just by name if it is in your PATH, then a new shell process will be created to interpret the script (the shell type is determined by the obscure #!/bin/xxx at the start of the script).

If you source a script either by using the source keyword or the . syntax, i.e. one of these:

source test.sh
. test.sh

...then the file will be read and executed in the current shell.

Here are some implications of using source over executing a script that i can think of:

  • Environment variables and other shell variables which are set in the script will still be available after the script has completed "executing".
  • If you call exit from the sourced script, it will terminate the calling script or shell.
  • You won't add the overhead of starting a new shell process. For few calls this might not interest you, but when looping a script many times the overhead adds up.

It looks like you want to source your script rather than executing it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top